RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /Python for AI — Zero to Useful
  6. /Ch. 16
Python for AI — Zero to Useful

16. Working with APIs

Chapter 16 of 36 · 20 min
KEY INSIGHT

AI APIs accept JSON and return JSON. `requests.post(url, json=payload)` serializes the dict to JSON and sets the Content-Type header. Always check `response.status_code` before processing.

HTTP Basics

AI services expose REST APIs. You send a request; you get a response. The requests library handles this in Python:

import requests

response = requests.get("https://api.example.com/models")
print(response.status_code)  # 200 = success
print(response.text)         # Raw response text

POST Requests

AI APIs typically use POST for completions:

import requests

url = "https://api.openai.com/v1/chat/completions"
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}
payload = {
    "model": "gpt-4",
    "messages": [
        {"role": "user", "content": "Hello"}
    ],
    "temperature": 0.7
}

response = requests.post(url, headers=headers, json=payload)
print(response.status_code)
print(response.json())

Response Handling

response = requests.post(url, headers=headers, json=payload)

if response.status_code == 200:
    data = response.json()
    print(data["choices"][0]["message"]["content"])
else:
    print(f"Error {response.status_code}: {response.text}")

Query Parameters

# GET with query parameters
params = {
    "model": "gpt-4",
    "limit": 10
}
response = requests.get(
    "https://api.openai.com/v1/models",
    headers=headers,
    params=params
)

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Use a public API (like JSONPlaceholder) to make a GET request, check the status code, and print the response JSON.

← Chapter 15
Data Cleaning with Pandas
Chapter 17 →
Rate Limiting and Retries