51cowork APIDocs
Open docs navigation

SDKs

cURL, Python, and TypeScript

Examples use the current public gateway https://api.51cowork.com through standard SDK custom-Base-URL support. Continue to select models dynamically through /v1/models.

Quick answer

OpenAI SDKs use https://api.51cowork.com/v1. Anthropic SDKs use https://api.51cowork.com. API Keys come from Dashboard and model IDs come from /v1/models.

HTTP
cURL
Python
openai / anthropic
TypeScript
openai / @anthropic-ai/sdk
Credentials
Environment variables
01

Shared environment

Terminalbash
export COWORK_GATEWAY_URL="https://api.51cowork.com"
export COWORK_API_KEY="<key-from-dashboard>"
export COWORK_MODEL="<model-from-gateway>"
02

cURL

OpenAI Chat Completionsbash
curl "$COWORK_GATEWAY_URL/v1/chat/completions" \
  -H "Authorization: Bearer $COWORK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "'"$COWORK_MODEL"'",
    "messages": [{"role": "user", "content": "Hello"}]
  }'
Anthropic Messagesbash
curl "$COWORK_GATEWAY_URL/v1/messages" \
  -H "x-api-key: $COWORK_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "'"$COWORK_MODEL"'",
    "max_tokens": 256,
    "messages": [{"role": "user", "content": "Hello"}]
  }'
03

Python

OpenAI SDK · pip install openaipython
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["COWORK_API_KEY"],
    base_url=os.environ["COWORK_GATEWAY_URL"].rstrip("/") + "/v1",
)
result = client.chat.completions.create(
    model=os.environ["COWORK_MODEL"],
    messages=[{"role": "user", "content": "Hello"}],
)
print(result.choices[0].message.content)
Anthropic SDK · pip install anthropicpython
import os
from anthropic import Anthropic

client = Anthropic(
    api_key=os.environ["COWORK_API_KEY"],
    base_url=os.environ["COWORK_GATEWAY_URL"],
)
message = client.messages.create(
    model=os.environ["COWORK_MODEL"],
    max_tokens=256,
    messages=[{"role": "user", "content": "Hello"}],
)
print(message.content[0].text)
04

TypeScript / JavaScript

OpenAI SDK · npm install openaitypescript
import OpenAI from 'openai'

const client = new OpenAI({
  apiKey: process.env.COWORK_API_KEY,
  baseURL: `${process.env.COWORK_GATEWAY_URL?.replace(/\/$/, '')}/v1`,
})
const result = await client.chat.completions.create({
  model: process.env.COWORK_MODEL!,
  messages: [{ role: 'user', content: 'Hello' }],
})
console.log(result.choices[0]?.message.content)
Anthropic SDK · npm install @anthropic-ai/sdktypescript
import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic({
  apiKey: process.env.COWORK_API_KEY,
  baseURL: process.env.COWORK_GATEWAY_URL,
})
const message = await client.messages.create({
  model: process.env.COWORK_MODEL!,
  max_tokens: 256,
  messages: [{ role: 'user', content: 'Hello' }],
})
console.log(message.content)
05

Streaming with an SDK

Use the SDK streaming API and consume each event or chunk. Configure connection and read timeouts, and abort the upstream request when the user cancels.

Protocol smoke testbash
curl -N "$COWORK_GATEWAY_URL/v1/chat/completions" \
  -H "Authorization: Bearer $COWORK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "'"$COWORK_MODEL"'",
    "stream": true,
    "messages": [{"role": "user", "content": "Count to five"}]
  }'
  • Do not buffer the entire response before forwarding it.
  • Do not log event content, prompts, or full Keys by default.
  • Retry disconnects cautiously to avoid duplicate calls and cost.
06

Before production

  • Inject the Key through a Secret Manager or deployment platform.
  • Set timeouts, cancellation, and bounded retries.
  • Create a business request ID and correlate it with Dashboard request_id.
  • Apply sensible input length and content boundaries.
  • Monitor balance and error rate, and replenish before exhaustion.