What is an API? A simple explanation, and how apps talk to AI
An API (application programming interface) is a set of requests one program can make to another, plus the rules for asking. When an app uses Claude, ChatGPT or Gemini behind the scenes, it sends a request to the model’s API over the internet and gets a response back. You prove who you are with a secret API key, and you pay for the tokens you send and receive.
- 1
So what is an API, in one picture?
Think of a restaurant. You don’t walk into the kitchen and cook. You read the menu, tell the waiter what you want, and the waiter brings your food back. The menu tells you what you can ask for. The waiter carries your order in and the answer out.
An API works the same way between two programs. One program (your app) asks. The other (a bank, a map service, an AI model) does the work and replies. MDN, the web’s main reference, calls an API "a simple contract" between the program offering it and the programs using it.
You already use APIs every day. When you pay with a UPI app, the app does not own your bank account. It sends a request in a format every bank agrees on, and your bank sends back "success" or "failed".
- You (the customer)
- Waiter (the API)
- Kitchen (the system)
- Food comes back (the response)
Key ideaAn API is a contract: ask in the agreed format, and you get an answer in the agreed format.
- 2
How does an app talk to an AI model?
Most AI apps you see, like a resume helper or a study bot, do not have their own model. They send your text to a model provider such as Anthropic (Claude), OpenAI (GPT) or Google (Gemini) through that provider’s API.
The request says which model to use, what the user wrote, and how long the answer may be. The response carries the model’s text plus a count of tokens used. Both are written in JSON, a simple text format of labels and values inside curly braces, like {"city": "Pune"}.
- Your app
- Request (JSON + API key)
- Model on the provider’s servers
- Response (JSON)
- Your app shows the answer
- 3
What does a real request look like?
Here is a request to Anthropic’s Messages API, trimmed to the parts that matter. The first line is the address (the endpoint). The next lines are headers: your key and the API version. Then comes the JSON body.
Read it like an order slip: which model, the most tokens the answer may use (max_tokens), and the conversation so far. Each message has a role ("user" for you, "assistant" for the model) and content.
A request to an AI model (Anthropic Messages API) POST https://api.anthropic.com/v1/messages Authorization: Bearer YOUR_SECRET_KEY anthropic-version: 2026-10-21 { "model": "claude-opus-5-5", "max_tokens": 1024, "messages": [ { "role": "user", "content": "Hello, Claude" } ] } - 4
What comes back, and what does it cost?
The reply is JSON too. The answer sits inside "content". The "usage" part tells you how many tokens went in and came out. That count is your bill. (New to tokens? See the lesson on tokens and context windows.)
Providers price input and output tokens separately, per million tokens. In November 2026, Anthropic lists Claude Sonnet 5.5 at $2 per million input tokens and $10 per million output tokens. So a call with 1,000 tokens in and 500 out costs $0.002 + $0.005 = $0.007, less than one rupee. Ten thousand such calls a day is about $70. Prices change, so always check the provider’s pricing page.
The response (from Anthropic’s docs) { "id": "msg_01XFDUDYJgAACzvnptvVoYEL", "type": "message", "role": "assistant", "content": [ { "type": "text", "text": "Hello!" } ], "model": "claude-opus-5-5", "stop_reason": "end_turn", "usage": { "input_tokens": 12, "output_tokens": 6 } }Key ideaYou pay per token, and output tokens usually cost more than input tokens.
- 5
What is an API key, and why must you hide it?
An API key is a long secret string that tells the provider "this request is from me, bill my account". Anyone who copies your key can make calls that you pay for. It is like your UPI PIN and your signature rolled into one.
That is why the key must live on a server you control, never inside a website’s JavaScript or a mobile app where anyone can dig it out. Anthropic’s guidance on keeping keys safe boils down to these habits:
- Never share your key, in chats, screenshots or emails. Each person gets their own.
- Load it from an environment variable, not typed into your code.
- Never commit a key to GitHub. Keep .env files out of your repository.
- Rotate keys on a schedule, for example every 90 days, and delete old ones.
- Turn on secret scanning so leaked keys are caught early.
Key ideaTreat an API key like a password that spends money. If it leaks, delete it and make a new one.
Straight from the source
The original idea, then the same idea taken apart, then how you use it today.
- 1 · Understand
- Instead of handing out the model, OpenAI offered it through an API with a general-purpose "text in, text out" interface. They gave three reasons: selling access pays for research, the models are too big and costly for most people to run, and a hosted API lets them stop misuse.
- 2 · Break it down
- "Text in, text out" is the whole contract. You send text, the model sends text back. That is still the shape of every AI API today.
- The model stays on the provider’s computers. You rent its work one request at a time, which is why you need a key and pay per use.
- Because the provider controls access, it can change prices, retire old models or block a key. Your app depends on their rules.
- 3 · Use it
- Treat every AI feature you build as "my text goes out, their text comes back". Keep your key on your server and check the provider’s model and pricing pages before you rely on them.
- 1 · Understand
- Karpathy describes an LLM as the core of a new kind of operating system. The model is like the kernel, its context window is like working memory (RAM), and tools such as search, a calculator or Python are like devices it can plug into.
- 2 · Break it down
- Kernel: the model is the part that thinks and decides. Everything else is arranged around it.
- RAM: the model only "sees" what is in its context window right now. When you call an API, that means only what is inside your request.
- Devices: tools let the model reach things it cannot do alone, like live data or exact maths.
- He also compares closed models (like GPT and Claude) and open models (like Llama) to the split between Windows and macOS on one side and Linux on the other.
- 3 · Use it
- When you call an AI API, remember you are filling the model’s working memory. With a Messages-style API like Anthropic’s, you send the whole conversation and any facts it needs in each request, because the model only works with what that request contains.
Words to know
- API
- Application programming interface: the agreed set of requests one program can make to another, and the format of the replies.
- Endpoint
- The web address you send a request to, such as https://api.anthropic.com/v1/messages.
- JSON
- A simple text format for data, made of labels and values inside curly braces. Almost all AI APIs use it.
- API key
- A secret string that identifies you to the provider and decides who gets billed. Keep it private.
- Token
- A small chunk of text (often part of a word). AI APIs count and charge by tokens in and tokens out.
- Environment variable
- A setting stored outside your code, used to keep secrets like API keys out of files you share.
Watch
Hand-picked from the people who explain this best. The video loads only when you press play.
The classic waiter analogy in three minutes. Watch it first, then come back to see how the same idea works for AI models.
Try it
Make your first API call from your phone
- 1Open the link below in your browser. It calls the free Open-Meteo weather API for Mumbai. What you see is the raw JSON response.
- 2Find "temperature_2m" inside "current". That is the live temperature. Everything else is labels and units.
- 3Change the latitude and longitude in the address bar to your city (search "<your city> latitude longitude") and reload. You just changed the request and got a new response.
- 4Notice there was no key. This weather API is public. AI APIs need a key because every call costs the provider money.
- 5Optional: in Google AI Studio, type a prompt, run it, then look for the "Get code" option. It shows the exact API request behind your prompt. Spot the model name, your text and where the key goes.
Check yourself
Pick an answer to see if you are right, and why.
Done? Keep going
Mark this lesson complete to keep your place. Progress is saved on this device, no login.
Questions people ask
What is an API in simple terms?
An API is a way for one program to ask another program for something, using fixed rules. Like ordering from a waiter with a menu: you ask in the agreed format, and you get a reply in the agreed format, without seeing how the kitchen works.
What is an API key?
An API key is a secret string you send with each request so the provider knows who is calling and whom to bill. Anyone with your key can spend your credits, so keep it on your server, out of code you share, and rotate it regularly.
How much does it cost to use an AI API?
AI APIs charge per token, with separate prices for input and output, quoted per million tokens. For example, in November 2026 Anthropic lists Claude Sonnet 5.5 at $2 per million input and $10 per million output tokens, so a short call costs a fraction of a rupee. Check the provider’s pricing page for current rates.
Sources and further reading
- MDN Web Docs: API (glossary)
- Anthropic docs: Working with the Messages API
- Anthropic API reference: Create a message
- Anthropic docs: Get started with Claude
- Anthropic docs: Pricing
- Claude Help Center: API key best practices
- OpenAI: OpenAI API (June 2020)
- Andrej Karpathy: [1hr Talk] Intro to Large Language Models (YouTube)