Using the OpenAI SDK with Swfte Connect: Python, JavaScript & curl
Swfte has no SDK package of its own. Point the OpenAI libraries or curl at the Swfte gateway.
Swfte does not publish an SDK package. The Connect gateway speaks the OpenAI chat-completions protocol, so the official OpenAI client libraries work unchanged once you set the base URL and use your Swfte API key. This guide covers client setup, your first call and the errors you can expect.
Installation
You install the OpenAI library, not a Swfte one.
# Python (3.9+)
pip install openai
Client Initialization
The gateway base URL is https://api.swfte.com/agents/v2/gateway. Authentication is an Authorization: Bearer sk-swfte-... header, which the OpenAI libraries send for you when you pass the key as api_key.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.swfte.com/agents/v2/gateway",
api_key=os.environ["SWFTE_API_KEY"],
)
Chat Completions
Model identifiers use the provider:model-name format. The Free plan can call mini-class models such as openai:gpt-4o-mini; premium models need a paid plan.
curl https://api.swfte.com/agents/v2/gateway/chat/completions \
-H "Authorization: Bearer $SWFTE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai:gpt-4o-mini",
"messages": [{"role": "user", "content": "Hello"}],
"max_tokens": 256
}'
Error Handling
Errors come back as JSON with an error object (message, type, code). The OpenAI libraries turn them into typed exceptions:
| Status | Meaning | What to do |
|---|---|---|
| 401 | Missing or invalid key (key_invalid, key_expired, key_disabled) | Check the sk-swfte- key and that it belongs to the right workspace |
| 402 | Plan or balance limit, for example a model your plan cannot call, or no credits left | Pick a mini model on Free, or upgrade or top up in the console |
| 429 | Rate limit for your plan | Back off and retry |
import openai
try:
response = client.chat.completions.create(
model="openai:gpt-4o-mini",
messages=[{"role": "user", "content": "Hello"}],
)
except openai.AuthenticationError:
print("Invalid API key -- check your credentials")
except openai.RateLimitError:
print("Rate limited -- back off and retry")
except openai.APIStatusError as e:
# 402 lands here: plan or balance limit
print(f"HTTP {e.status_code}: {e.response.text}")
except openai.APIConnectionError:
print("Could not reach the gateway")
Next Steps
- Getting Started -- First-time setup
- Multi-Provider Routing -- Failover and intelligent routing
- Cost Optimization -- Budget controls and cost strategies
- API Reference -- Raw REST API documentation