Lesson 13 of 20

Working with APIs

HTTP Basics

2 min read

HTTP (HyperText Transfer Protocol) is how your code talks to AI services. Understanding it is essential for working with any API.

The Request-Response Model

Your Code  ──[Request]──>  API Server
           <──[Response]──

Every API interaction follows this pattern:

  1. Your code sends a request
  2. The server processes it
  3. The server returns a response

HTTP Methods

MethodPurposeAI Use Case
GETRetrieve dataList models, check status
POSTSend dataChat completions, embeddings
PUTUpdate dataUpdate fine-tuned models
DELETERemove dataDelete files, cancel jobs

Most AI API calls use POST because you're sending prompts/messages.

Request Structure

POST /v1/chat/completions HTTP/1.1
Host: api.openai.com
Authorization: Bearer sk-your-key
Content-Type: application/json

{
    "model": "gpt-4",
    "messages": [{"role": "user", "content": "Hello!"}]
}

Key components:

  • Method + Path: What action and where
  • Headers: Metadata (auth, content type)
  • Body: The actual data (JSON for AI APIs)

Response Structure

HTTP/1.1 200 OK
Content-Type: application/json

{
    "choices": [
        {"message": {"content": "Hello! How can I help?"}}
    ]
}

HTTP Status Codes

CodeMeaningWhat to Do
200SuccessProcess the response
400Bad requestCheck your request format
401UnauthorizedCheck API key
429Rate limitedWait and retry
500Server errorRetry or contact support

Headers You'll Use Often

headers = {
    "Authorization": "Bearer sk-your-api-key",
    "Content-Type": "application/json",
    "Accept": "application/json"
}

Next, we'll use the requests library to make these calls in Python. :::

Quick check: how does this lesson land for you?

Quiz

Module 4: Working with APIs

Take Quiz
FREE WEEKLY NEWSLETTER

Stay on the Nerd Track

One email per week — courses, deep dives, tools, and AI experiments.

No spam. Unsubscribe anytime.