Compatibility Mode Invocation (SDK Integration)
4ALL API fully implements the OpenAI interface protocol, so the official SDK and all OpenAI-compatible clients can be used directly, with no code changes required—even when calling non-OpenAI models such as Claude and Gemini, requests and responses are unified into the OpenAI format.
Python
from openai import OpenAI
client = OpenAI( base_url="https://api.4allapi.com/v1", api_key="sk-你的令牌",)
resp = client.chat.completions.create( model="claude-sonnet-5", # 换成任意在售模型即可 messages=[{"role": "user", "content": "你好"}],)print(resp.choices[0].message.content)Node.js
import OpenAI from 'openai';
const client = new OpenAI({ baseURL: 'https://api.4allapi.com/v1', apiKey: 'sk-你的令牌',});
const resp = await client.chat.completions.create({ model: 'gpt-5.5', messages: [{ role: 'user', content: 'Hello' }],});console.log(resp.choices[0].message.content);Environment Variable Method
Most tools support environment variables, so there is no need to modify the code:
export OPENAI_BASE_URL="https://api.4allapi.com/v1"export OPENAI_API_KEY="sk-你的令牌"Notes
- To switch models across vendors, only the
modelfield needs to be changed; the message format remains unchanged; - Streaming (
stream: true), function calling (tools), visual input, and other capabilities are also compatible, see Streaming Output and FC Function Calling; - If you need the native Anthropic / Google protocols, see Claude Native Invocation, Gemini Native Invocation。