Build with the API
Quickstart: your first API request
This page takes you from nothing to a working request. You need an account with credit and a way to call it: curl, or the Python or Node client you already use.
1. Create a key#
Sign up if you have not, add credit under Billing, then open API keys and choose Create. Give the key a name, such as build-server, and copy it when it is shown: it is shown once. Keep it out of source code and put it in your environment.
export SVEN_API_KEY="paste-your-key-here"
Only an admin of an organisation can create keys; in a personal account you can. See Authentication and API keys.
2. Install a client#
Skip this for curl. The clients you already have for chat work as they are: this API is the one they talk to once you change their base URL and key.
pip install openai
3. Choose a model#
List the models your key can call and pick a chat model's id. Prices are on the pricing page.
curl https://sven.swedishembedded.com/v1/models \
-H "Authorization: Bearer $SVEN_API_KEY"
In the examples below, replace MODEL_ID with the id you chose.
4. Send your first request#
curl https://sven.swedishembedded.com/v1/chat/completions \
-H "Authorization: Bearer $SVEN_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "MODEL_ID", "messages": [{"role": "user", "content": "Hello"}]}'
The answer is the usual chat completion object. Your charge appears in Usage and logs as soon as the call finishes.
5. Stream the answer#
Add stream and the reply arrives as it is written.
curl -N https://sven.swedishembedded.com/v1/chat/completions \
-H "Authorization: Bearer $SVEN_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "MODEL_ID",
"messages": [{"role": "user", "content": "Write a haiku about the sea"}],
"stream": true
}'
More in Streaming.
Where to go next#
- Authentication and API keys: restrict and cap your keys.
- Errors and Rate limits: what to do when a call is refused.
- Safe retries: retry without paying twice.
- API reference: every route.