All endpoints live under /v1. Auth via the Authorization: Bearer sk-... header.
With the OpenAI protocol, base_url includes /v1 (https://www.fortapi.com/v1). With the Anthropic SDK, set ANTHROPIC_BASE_URL to the bare domain without /v1 (https://www.fortapi.com) — the SDK appends /v1/messages itself, and adding /v1 yourself causes a 404.
from openai import OpenAIclient = OpenAI( api_key="sk-YOUR_KEY", base_url="https://www.fortapi.com/v1",)response = client.chat.completions.create( model="your-model-name", # see the pricing page on the main site for available model names messages=[ {"role": "system", "content": "You are a friendly assistant."}, {"role": "user", "content": "Hello! What can you do?"}, ], temperature=0.7,)print(response.choices[0].message.content)print(f"Tokens used: {response.usage.total_tokens}")
import OpenAI from "openai";const client = new OpenAI({ apiKey: "sk-YOUR_KEY", baseURL: "https://www.fortapi.com/v1",});const response = await client.chat.completions.create({ model: "your-model-name", // call Claude through OpenAI protocol; see the pricing page on the main site for available model names messages: [ { role: "user", content: "Explain quantum entanglement simply." }, ],});console.log(response.choices[0].message.content);
Replace your-model-name with any model name from the pricing page on the main site.
# replace "your-model-name" with any model name from the pricing pagecurl https://www.fortapi.com/v1/chat/completions \ -H "Authorization: Bearer sk-YOUR_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "your-model-name", "messages": [{"role": "user", "content": "Write a haiku about code."}], "stream": true }'
from anthropic import Anthropicclient = Anthropic( api_key="sk-YOUR_KEY", base_url="https://www.fortapi.com",)response = client.messages.create( model="your-model-name", # see the pricing page on the main site for available model names max_tokens=1024, system="You are a technical expert.", messages=[ {"role": "user", "content": "What is the CAP theorem?"}, ],)print(response.content[0].text)print(f"Used: input={response.usage.input_tokens}, output={response.usage.output_tokens}")
import Anthropic from "@anthropic-ai/sdk";const client = new Anthropic({ apiKey: "sk-YOUR_KEY", baseURL: "https://www.fortapi.com",});const response = await client.messages.create({ model: "your-model-name", // see the pricing page on the main site for available model names max_tokens: 1024, messages: [ { role: "user", content: "Explain Rust's borrow checker." }, ],});console.log(response.content[0].text);
With stream: true, the response is a series of Server-Sent Events: each line starts with data: and ends with a final data: [DONE]. The official SDKs parse this for you — just iterate.
stream = client.chat.completions.create( model="your-model-name", # see the pricing page on the main site for available model names messages=[{"role": "user", "content": "Count from 1 to 5"}], stream=True,)for chunk in stream: delta = chunk.choices[0].delta.content if delta: print(delta, end="", flush=True)
const stream = await client.chat.completions.create({ model: "your-model-name", // see the pricing page on the main site for available model names messages: [{ role: "user", content: "Count from 1 to 5" }], stream: true,});for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content || "");}
The model decides which function to call and returns the arguments; you run the function locally, then send the result back as a role: "tool" message, and the model produces the final answer.
tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Get the current weather for a city", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, },}]messages = [{"role": "user", "content": "What's the weather in Beijing right now?"}]resp = client.chat.completions.create( model="your-model-name", # use a tool-calling capable model; see the pricing page messages=messages, tools=tools, tool_choice="auto",)msg = resp.choices[0].messageif msg.tool_calls: call = msg.tool_calls[0] # run the function yourself (mock data here) and send the result back messages.append(msg) messages.append({ "role": "tool", "tool_call_id": call.id, "content": '{"temp": "26°C", "weather": "sunny"}', }) final = client.chat.completions.create(model="your-model-name", messages=messages) print(final.choices[0].message.content)
emb = client.embeddings.create( model="your-model-name", # use an embedding model; see the pricing page input="text to embed",)print(len(emb.data[0].embedding))
img = client.images.generate( model="your-model-name", # use an image model; see the pricing page prompt="a shiba inu coding at a laptop, flat illustration", size="1024x1024",)print(img.data[0].url)
The full list with current models is on the Pricing page on the main site. Which models each endpoint supports is defined by the Pricing page and the Models list in the console.