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

browseryour serverAI API
the key lives on your server; the browser never sees it

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 second

max_tokens

A hard cap on reply length. Protects users from novels and you from invoices.

512 covers most answers

how it works

  1. 1
    Get a key, keep it secret

    Create an API key, put it in a server-side environment variable, and gitignore the file.

  2. 2
    Build the messages array

    Start with one system message for behavior, then the user message. That is the whole request body.

  3. 3
    Send the POST request

    One HTTPS call with model, messages, and max_tokens. No SDK required for your first call.

  4. 4
    Stream 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

1const res = await fetch(url, {
one POST with JSON — no SDK needed
2 headers: { "x-api-key": process.env.KEY },
the key stays server-side, in env vars
3 body: JSON.stringify({
4 model: "claude-sonnet-4-5",
5 max_tokens: 512,
caps the reply and the bill
6 messages: [{ role: "user", content }],
role + content — that is the whole protocol
7 }),
8})

side by side

streaming

  • first words in ~1 second
  • users read while it writes
  • feels dramatically faster
  • same total cost and tokens
VS

wait for full reply

  • simple request-response code
  • blank screen for 10-30s
  • users assume it froze
⚠️ watch out

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

📚 tutorial

What is in an LLM request?watch ↗

🔧 useful

Where do API keys go?watch ↗

💎 deep

Why does streaming feel 10x faster?watch ↗

💎 deep

Why set max tokens?watch ↗

🧠 trivia

What paper made LLMs mainstream?watch ↗