Velamuvi AIAcademy

Lesson 3 of 8 · Basics

Your First API Call

Make your first AI API call: get a key, set an environment variable, install an SDK and send a message, with three providers, plain HTTP and a live request.

  • 7 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • The setup steps before your first call
  • How to call a model with an official SDK
  • What the same call looks like as plain HTTP
  • How to see exactly what your program sends

Before you start

  1. Create an account with a provider and open its developer console.
  2. Add billing or credit. Most APIs need prepaid credit or a payment method; some offer a limited free tier. Set a spending limit straight away.
  3. Create an API key and copy it once into a safe place.
  4. Set it as an environment variable in your terminal (never in your code):
# macOS or Linux
export ANTHROPIC_API_KEY="your-key-here"

# Windows PowerShell
$env:ANTHROPIC_API_KEY="your-key-here"
  1. Install the SDK for your provider, for example pip install anthropic, pip install openai or pip install google-genai.

These examples run on your own computer, not in this page, because they need your key. The model name is a placeholder: copy a current one from the provider's list of models.

With an SDK

Anthropic (Claude):

import anthropic

client = anthropic.Anthropic()            # reads ANTHROPIC_API_KEY from the environment

message = client.messages.create(
    model="MODEL_NAME",                   # copy a current model name from the docs
    max_tokens=300,
    system="You are a friendly tutor for beginners.",
    messages=[{"role": "user", "content": "Explain recursion in two sentences."}],
)
print(message.content[0].text)

OpenAI:

from openai import OpenAI

client = OpenAI()                         # reads OPENAI_API_KEY from the environment

response = client.responses.create(
    model="MODEL_NAME",
    instructions="You are a friendly tutor for beginners.",
    input="Explain recursion in two sentences.",
)
print(response.output_text)

Google Gemini:

from google import genai

client = genai.Client()                   # reads GEMINI_API_KEY from the environment

response = client.models.generate_content(
    model="MODEL_NAME",
    contents="Explain recursion in two sentences.",
)
print(response.text)

Three providers, three slightly different shapes, one pattern: create a client, call a method with the model and the input, read the text.

The same call as plain HTTP

Under the hood, an SDK sends an ordinary HTTPS request. Here is the Anthropic call above with requests:

import os
import requests

response = requests.post(
    "https://api.anthropic.com/v1/messages",
    headers={
        "x-api-key": os.environ["ANTHROPIC_API_KEY"],
        "anthropic-version": "2023-06-01",
        "content-type": "application/json",
    },
    json={
        "model": "MODEL_NAME",
        "max_tokens": 300,
        "messages": [{"role": "user", "content": "Explain recursion in two sentences."}],
    },
    timeout=60,
)
response.raise_for_status()
print(response.json()["content"][0]["text"])

Seeing the raw request makes error messages and documentation much easier to understand, even if you use the SDK every day.

See what your program sends

You can't call a real AI API from this page without a key, but you can send the same kind of request to a test server that echoes it back. This shows the request from the server's side:

Try it yourselfPython
import requests

# httpbin.org echoes back exactly what it receives, so we can see our request
# from the server's side. No AI here, and the key is fake.
body = {
    "model": "MODEL_NAME",
    "max_tokens": 100,
    "messages": [{"role": "user", "content": "Say hello in French."}],
}
headers = {"Authorization": "Bearer sk-fake-key-for-testing"}

response = requests.post("https://httpbin.org/post", json=body, headers=headers, timeout=10)
print("Status:", response.status_code)

received = response.json()
print("The server saw this header:", received["headers"]["Authorization"])
print("And this JSON body:", received["json"])

The AI provider receives exactly this: a header carrying the key and a JSON body with the model and messages. It checks the key, runs the model and replies.

Common mistakes

Practice exercise

Prepare the body for a call to an API that keeps the system prompt in its own field.

Your solutionPython
import json

question = "What is the capital of Japan?"
system = "Answer in one word."

# Build the request body for a provider that takes the system prompt as a
# separate "system" field (not inside messages). Use:
#   "model": "MODEL_NAME", "max_tokens": 50, "system": system,
#   "messages": a list with one user message containing the question
# Then print it with json.dumps(body)
Knowledge check

Your first call fails with status 401. What is the most likely cause?

Summary

  • Setup: account, billing and a spending limit, a key, an environment variable, the SDK.
  • With an SDK, a call is: create a client, call one method with the model and input, read the text.
  • Underneath is a plain HTTPS POST with a key header and a JSON body.
  • First-call errors are usually the key (401), the model name (404) or a body field (400); read the error message.

Next: reading everything that comes back in the response.

Finished reading? Tick it off to track your progress.