Velamuvi AIAcademy

Lesson 1 of 8 · Basics

How AI APIs Work

The pattern behind OpenAI, Anthropic and Google AI APIs: an HTTPS request with a key, a JSON body of model, messages and settings, a JSON reply, and SDKs.

  • 6 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • The round trip of an AI API call, step by step
  • What goes in the request: endpoint, headers and JSON body
  • How the major providers differ, and what they share
  • What an SDK is and why you will usually use one

Chat apps are built on APIs

When you type into a chat app, the app sends your message to a model over the internet and shows you the reply. An AI API lets your own programs do the same: summarise documents, sort emails, answer customers or power a feature in your app.

This course assumes you know the basics of HTTP, JSON and status codes from the APIs and HTTP course. Everything there applies here.

The round trip

  1. Your program builds a request: which model, the messages, and settings such as the maximum length.
  2. It sends the request over HTTPS to the provider's endpoint, with your API key in a header.
  3. The provider checks the key, runs the model, and counts the tokens.
  4. It returns a response in JSON: the generated text, why it stopped, and how many tokens were used.
  5. Your program reads the parts it needs.

What a request looks like

Try it yourselfPython
import json

question = "Explain what an API is in one sentence."

# The request body: the same idea at every provider, with small differences
request_body = {
    "model": "MODEL_NAME",          # which model to use (copy from the provider's docs)
    "max_tokens": 200,              # upper limit on the length of the answer
    "temperature": 0.3,             # how random the sampling is
    "messages": [
        {"role": "system", "content": "You are a patient teacher for beginners."},
        {"role": "user", "content": question},
    ],
}

headers = {
    "Authorization": "Bearer YOUR_API_KEY",   # who you are (never type a real key into code)
    "Content-Type": "application/json",       # the body is JSON
}

print("POST https://api.example-ai.com/v1/chat")
for name in headers:
    print(f"{name}: {headers[name]}")
print()
print(json.dumps(request_body, indent=2))

This is a made-up provider, but every major API asks for the same ingredients: who you are (the key), which model, the conversation and settings.

Same idea, different details

Each provider has its own small differences. At the time of writing:

OpenAIAnthropicGoogle Gemini
Key headerAuthorization: Bearer ...x-api-key: ... plus a version headerx-goog-api-key: ...
Main text endpointResponses (and Chat Completions)MessagesgenerateContent
System promptA message with a system or developer roleA separate system fieldA separate system instruction
Length limitOptionalmax_tokens is requiredOptional
Where the text isIn the output or choices listIn a list of content blocksIn a list of candidates

Details change over time, so always check the provider's current documentation. The pattern stays the same.

SDKs: the easy way

You can call an AI API with plain HTTP using requests, and it is worth seeing once, because it shows what is really happening. In real projects, use the provider's SDK: it is shorter, safer and kept up to date when the API changes.

Common mistakes

Practice exercise

Complete the request body an AI API needs for a one-question conversation.

Your solutionPython
import json

body = {
    "model": "MODEL_NAME",
    "max_tokens": 100,
    "messages": [],
}

# Add one user message with the content "What is 2 + 2?" to body["messages"],
# then print the body as JSON on one line with json.dumps(body)
Knowledge check

Which ingredients does almost every AI API request contain?

Summary

  • An AI API lets your program send input to a hosted model and get output back, billed per token.
  • Requests go to an endpoint with your key in a header and a JSON body: model, messages, settings.
  • Providers differ in header names, endpoints and response shapes, but share the same pattern.
  • SDKs wrap the HTTP details; use them in real projects.
  • APIs are stateless: each request carries everything the model needs.

Next: API keys, and how to keep them secret.

Finished reading? Tick it off to track your progress.