Using AI through an API.
Apps like chatbots, writing tools and smart search are built by connecting to AI models through an API. Today you'll understand how APIs work, how developers call an AI model from code, and how to keep keys and costs safe.
What is an API?
An API (Application Programming Interface) is a way for one program to ask another program for something. Think of a restaurant: you (the app) give your order to the waiter (the API), the kitchen (the AI model) prepares it, and the waiter brings back the dish (the response).
When a food app shows a map, it's using a maps API. When an app has an "AI assistant" button, it's often calling an AI company's API, such as those from Anthropic (Claude), OpenAI or Google (Gemini).
The basic flow
- Create an account with an AI provider's developer platform and get an API key (a secret password for your app).
- Your code sends a request: the model name, your prompt, and settings.
- The provider sends back a response: the generated text.
- You pay based on tokens used (input + output), usually a small amount per thousand or million tokens.
What a request looks like (general shape)
import os, requests
API_KEY = os.environ["AI_API_KEY"] # never write the key in the code!
response = requests.post(
"https://api.example-ai-provider.com/v1/messages",
headers={"Authorization": "Bearer " + API_KEY},
json={
"model": "model-name",
"max_tokens": 300,
"messages": [
{"role": "user", "content": "Write 3 short captions for a Hyderabad bakery's new cake."}
],
},
)
print(response.json())
Each provider's exact address and format differ slightly; their official documentation shows the correct example, and they also offer ready-made Python libraries. The idea is always the same: send messages, get a reply.
Keep keys and costs safe
What people build with AI APIs
- Customer support bots trained on a company's FAQ.
- Tools that summarise PDFs or meeting notes.
- Apps that answer questions using a company's own documents (often called RAG: retrieval-augmented generation). Our AI Scholar works on a similar idea, finding sources first and then answering.
Where should an API key be stored?
Keys are passwords. Keep them out of code and chats.
Module 3 test
In Python, what does print(len(["a","b","c"])) show?
len counts items.
In a dataset, the "label" is…
Features in, label out.
Predicting whether an email is spam is…
Categories → classification.
Why split data into train and test sets?
Testing on new data shows real performance.
What do AI APIs usually charge for?
Usage is measured in tokens.
Your task (20 min)
Visit the developer documentation of one AI provider (for example docs.anthropic.com, platform.openai.com/docs or ai.google.dev). Find the "quickstart" page and read the first example. Identify: where the key goes, where the prompt goes, and where the answer comes back. You don't need to pay or run it.
Lesson 12 complete!
You understand APIs, keys, requests and responses, tokens and cost, and safe practices. You finished Module 3!
Next: Ethics, bias and privacy →