Your First AI API Call
One fetch call. Now your app talks back.
▶ the lesson · narrated sketch video · tap for sound
the intro
Calling an LLM is a normal HTTP request: you send a list of messages with roles, and the model responds with generated text. Add streaming for instant output. The skills to learn: message structure, streaming, and keeping API keys on the server.
fun fact ✦
The messages array is just chat history — role plus content, repeated. That is the whole protocol.
the docs
the deep dive
the ideas behind the video — concepts, diagrams, a walkthrough and the traps to avoid
Under the hype, calling an LLM is a boring HTTPS request: JSON in, JSON out. The hard parts are not the call — they are the three things tutorials skip: where the key lives, why streaming changes everything, and how the bill is counted. Get those right on day one and the rest is easy.
a boring HTTPS request
key terms
messages array
The whole protocol: a list of role-plus-content turns the model reads and extends.
[{role: "user", content: "hi"}]system prompt
Standing instructions that set behavior before the user says a word.
"always answer in Spanish"streaming
Tokens arrive as they are generated, so the UI renders while the model is still writing.
first token lands in ~1 secondmax_tokens
A hard cap on reply length. Protects users from novels and you from invoices.
512 covers most answershow it works
- 1Get a key, keep it secret
Create an API key, put it in a server-side environment variable, and gitignore the file.
- 2Build the messages array
Start with one system message for behavior, then the user message. That is the whole request body.
- 3Send the POST request
One HTTPS call with model, messages, and max_tokens. No SDK required for your first call.
- 4Stream the reply
Render tokens as they arrive instead of waiting for the full text. Same cost, ten times the feel.
the code, explained
the whole call is one POST
side by side
streaming
- first words in ~1 second
- users read while it writes
- feels dramatically faster
- same total cost and tokens
wait for full reply
- simple request-response code
- blank screen for 10-30s
- users assume it froze
An API key in frontend code is public the moment you deploy — view source, done. Keys live in server-side env vars, and your frontend talks to your server, which talks to the AI API. Rotate any key that has ever touched client code.
quick answers
Which provider should I start with?
Any major one. The messages pattern transfers between them — swap SDKs later, the concepts are identical.
How much does one call cost?
Rough numbers: small models well under $1 per million input tokens, frontier models several times that. A typical chat reply costs a fraction of a cent.
Can I call it from the browser?
Not with your key. Browsers cannot keep secrets — proxy through your server, which injects the key.
quick hits about Your First AI API Call
short answers · interviews, tips, trivia & hidden gems