cheat-sheets

OpenAI API

The OpenAI HTTP API with curl and the Python SDK, plus how to point OpenAI-compatible clients at local servers such as Ollama and llama.cpp. For the Anthropic equivalent see Claude API and SDKs.

Documentation: OpenAI API reference · Python SDK

Verification: request shapes are from the official docs as remembered and were not executed (no key used). Model names are placeholders; list yours with GET /v1/models. Items marked (unverified) need confirming.

Setup #

bash
export OPENAI_API_KEY="YOUR_API_KEY"
COMMANDDESCRIPTION
pip install openai / uv add openaiPython SDK
npm install openaiJavaScript SDK
curl --oauth2-bearer "$OPENAI_API_KEY" https://api.openai.com/v1/modelsList models (curl sends the bearer token for you)

Chat Completions #

bash
curl https://api.openai.com/v1/chat/completions \
  --oauth2-bearer "$OPENAI_API_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "MODEL_NAME",
    "messages": [
      {"role": "system", "content": "You are terse."},
      {"role": "user", "content": "Explain DNS in one sentence."}
    ]
  }'
python
from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY
resp = client.chat.completions.create(
    model="MODEL_NAME",
    messages=[{"role": "user", "content": "Explain DNS in one sentence."}],
)
print(resp.choices[0].message.content)

Responses API #

OpenAI’s newer interface, with built-in tools and conversation state (unverified: check current guidance on which API to use).

bash
curl https://api.openai.com/v1/responses \
  --oauth2-bearer "$OPENAI_API_KEY" \
  -H "content-type: application/json" \
  -d '{"model": "MODEL_NAME", "input": "Explain DNS in one sentence."}'

Endpoints #

ENDPOINTUSE
POST /v1/chat/completionsChat; the de facto standard that many servers copy
POST /v1/responsesNewer unified interface
POST /v1/embeddingsEmbeddings
GET /v1/modelsList models
POST /v1/batchesAsynchronous batch jobs (unverified)

OpenAI-Compatible Local Servers #

Most clients let you override the base URL. Local servers usually ignore the key but clients still require one, so any non-empty string works.

SERVERBASE URL
Ollamahttp://localhost:11434/v1
llama.cpp llama-serverhttp://localhost:8080/v1
LM Studiohttp://localhost:1234/v1
python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:11434/v1", api_key="unused")
resp = client.chat.completions.create(
    model="llama3.2:3b",
    messages=[{"role": "user", "content": "Hello"}],
)
bash
export OPENAI_BASE_URL="http://localhost:11434/v1"
export OPENAI_API_KEY="unused"

OPENAI_BASE_URL is read by the official Python SDK; other tools may use a different name.

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.