← The journal
Client Setup

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 Journal / Client Setup

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:

StatusMeaningWhat to do
401Missing or invalid key (key_invalid, key_expired, key_disabled)Check the sk-swfte- key and that it belongs to the right workspace
402Plan or balance limit, for example a model your plan cannot call, or no credits leftPick a mini model on Free, or upgrade or top up in the console
429Rate limit for your planBack 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

Keep the conversation practical.

Turn an idea into a working next step.

Discuss your use case
0
0
0
0

Enjoyed this article?

Get more insights on AI and enterprise automation delivered to your inbox.

Deploy a model with Swfte Connect

One gateway, every provider, per-token cost visibility. Swap models without touching your code.