Skip to main content

Get Your API Key

1

Sign in to Switchport

Go to switchport.ai and log in to your account.
2

Navigate to API Keys

Go to SettingsAPI Keys in the dashboard.
3

Copy your API key

Copy your API key. It should start with sp_.

Set Your API Key

There are two ways to authenticate with the Switchport API: Set the SWITCHPORT_API_KEY environment variable:
export SWITCHPORT_API_KEY=sp_your_key_here
Then initialize the client without passing the API key:
from switchport import Switchport

client = Switchport()  # Automatically reads from environment

Option 2: Pass API Key Directly

Pass the API key when creating the client:
from switchport import Switchport

client = Switchport(api_key="sp_your_key_here")
Never hard-code API keys in your source code. Use environment variables or a secrets management system instead.

Using .env Files

For local development, you can use a .env file with a library like python-dotenv:
  1. Install python-dotenv:
pip install python-dotenv
  1. Create a .env file in your project root:
SWITCHPORT_API_KEY=sp_your_key_here
  1. Load the environment variables in your code:
from dotenv import load_dotenv
from switchport import Switchport

load_dotenv()  # Load .env file

client = Switchport()  # Reads from environment
Add .env to your .gitignore file to avoid committing secrets to version control.

Configure API URL (Optional)

By default, the SDK connects to https://switchport-api.vercel.app. For local development or self-hosted instances, you can override this:

Using Environment Variable

export SWITCHPORT_API_URL=http://localhost:8001

Passing to Client

client = Switchport(
    api_key="sp_your_key_here",
    api_url="http://localhost:8001"
)

Error Handling

If authentication fails, the SDK raises an AuthenticationError:
from switchport import Switchport, AuthenticationError

try:
    client = Switchport()
    response = client.prompts.execute("my-prompt")
except AuthenticationError as e:
    print(f"Authentication failed: {e}")
    # Handle error (e.g., prompt user to check API key)
Common authentication errors:
  • Invalid API key: The API key is incorrect or has been revoked
  • Missing API key: No API key provided and SWITCHPORT_API_KEY is not set
  • Expired API key: The API key has expired (contact support)

Security Best Practices

Store API keys in environment variables, not in source code.
Regularly rotate API keys to minimize risk if a key is compromised.
Use separate API keys for development, staging, and production.
If available, use API keys with limited permissions for specific use cases.
Add .env and secrets files to .gitignore.

Next Steps

Execute Prompts

Learn how to execute prompts with the SDK