API (Application Programming Interface) is a set of rules that allows different software applications to communicate with each other. Think of it as a waiter in a restaurant – you give an order (request), the waiter takes it to the kitchen (API), and brings back your food (response).
When you use ChatGPT, you're actually using an API behind the scenes. Your prompts are sent to OpenAI's servers, processed by GPT-4, and the response comes back. You don't need to run the model on your computer!
Most AI APIs use REST architecture. Key concepts:
| Method | Use with AI APIs | Example |
|---|---|---|
| GET | Retrieve model info, check usage | GET /v1/models |
| POST | Send prompts, get completions | POST /v1/chat/completions |
Most AI APIs require an API key – a unique secret string that identifies you and tracks your usage.
# Example API key header
Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxx
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"model": "gpt-4",
"messages": [{"role": "user", "content": "What is an LLM?"}],
"temperature": 0.7
}'
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY")
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "What is an LLM?"}],
temperature=0.7
)
print(response.choices[0].message.content)
import OpenAI from 'openai';
const openai = new OpenAI({ apiKey: 'YOUR_API_KEY' });
async function main() {
const completion = await openai.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'What is an LLM?' }],
temperature: 0.7,
});
console.log(completion.choices[0].message.content);
}
main();
| Model | Input Price (per 1M tokens) | Output Price (per 1M tokens) |
|---|---|---|
| GPT-4 Turbo | $10.00 | $30.00 |
| GPT-3.5 Turbo | $0.50 | $1.50 |
| Claude 3 Sonnet | $3.00 | $15.00 |
| Claude 3 Haiku | $0.25 | $1.25 |
What would be the HTTP status code for each situation?
List three security best practices for handling API keys.
Sign up for a free API key from OpenAI, Anthropic, or Google and make your first API call. What response did you get?