Generate a new primary API key for your account. This immediately invalidates the current API key and replaces it with a fresh one. This operation is useful for security key rotation or when you suspect your current key has been compromised.
Immediate Invalidation: Your current API key stops working immediately after regeneration. All applications using the old key will start receiving authentication errors until updated.
import httpximport osimport timedef regenerate_api_key_safely(current_key: str) -> str: """Safely regenerate API key with confirmation""" print("⚠️ WARNING: This will invalidate your current API key!") print("Make sure you can update all applications immediately.") confirm = input("Type 'REGENERATE' to confirm: ") if confirm != "REGENERATE": print("❌ Key regeneration cancelled") return current_key client = httpx.Client() try: response = client.get( "https://api.electronhub.ai/v1/auth/key/regen", headers={"Authorization": f"Bearer {current_key}"} ) response.raise_for_status() result = response.json() new_key = result["key"] print(f"✅ New API key generated: {new_key}") print("🔄 Old key is now invalid") # Optionally save to environment file save_to_env = input("Save to .env file? (y/n): ") if save_to_env.lower() == 'y': with open('.env', 'w') as f: f.write(f"ELECTRONHUB_API_KEY={new_key}\n") print("💾 Saved to .env file") return new_key except Exception as e: print(f"❌ Error regenerating key: {e}") return current_key# Usagecurrent_key = os.getenv("ELECTRONHUB_API_KEY")new_key = regenerate_api_key_safely(current_key)