What Is an API, and How Does It Work?
Understand how an app talks to a model over the internet: requests, responses, keys and errors.
Your app runs on a phone or a website; the model runs on GPUs in someone’s data centre. They talk through an API, an Application Programming Interface: an agreed way for one program to ask another for something.
A restaurant is a good picture. You don’t walk into the kitchen; you give your order to a waiter, in a form the kitchen understands, and a dish comes back. The menu tells you what you may order. An AI API works the same way. Your app sends a request over the internet to an address such as https://api.example-ai.com/v1/chat, and gets a response back.
The request is a small, structured message, usually written in a format called JSON. It typically says:
- which model to use;
- the messages: instructions plus the conversation so far, each labelled with a role;
- settings, such as the maximum length of the reply or the temperature from Track 2;
- an API key: a secret password that identifies who is asking, and who pays.
The response carries a status (200 means OK), the model’s text, the reason it stopped (finished naturally, or hit the length limit), and how many tokens went in and out, which is what you’re billed for.
Things go wrong too, and the status code says how. 401: missing or wrong key. 429: too many requests too fast, a rate limit, so wait and try again. 500-something: a problem on the provider’s side. Well-built apps expect all of these and handle them gracefully, because at scale every one of them happens daily.
Key takeaways
- An API is an agreed way for programs to talk: your app sends a request, the provider sends a response.
- A chat request names the model, carries role-labelled messages and settings, and includes a secret API key.
- The response includes a status, the text, why it stopped and the tokens used (what you pay for).
- Errors are normal: 401 = bad key, 429 = too many requests, 5xx = provider trouble. Apps must handle them.
Check your understanding
Go deeper (more technical)
Most AI APIs are HTTP APIs: the same protocol web pages use. The request is an HTTP POST with JSON in its body; the key travels in a header. Providers publish client libraries that wrap these details, and many follow a similar “chat completions” shape, so apps can switch providers with small changes.
API keys must never be put in code that ships to users’ phones or browsers, where anyone can extract them. Apps keep keys on their own servers, which call the AI provider on the user’s behalf. Retrying after errors usually uses exponential backoff: wait 1 second, then 2, then 4, so a struggling service isn’t flooded.