Calling a model through an API

AI & LLM fundamentals · Essentials · 12 min

Every AI feature outside a chat window, including automation steps, works by sending API requests, so this unlocks the rest.

Chat apps are one way to use a model. Programs use it through an API (application programming interface): a fixed way for one program to send a question to another service over the internet and get an answer back. Think of a restaurant order form: you fill in agreed fields, hand it over, and get a dish back.

The pieces:

  • Request: the message your program sends. It names the model to use and contains the input (instructions and messages), plus optional settings such as maximum output tokens.
  • Response: what comes back: the model's text, why it stopped (finished, or hit the token limit), and how many input and output tokens were used.
  • API key: a long secret string, like a password, that you get from the provider's dashboard. It is sent with every request so the provider knows who is calling and whom to bill. Never paste it into a shared document, a screenshot or code you publish. If it leaks, delete it in the dashboard and create a new one.
  • JSON: the text format used for requests and responses. It is made of "name": value pairs inside curly braces, and lists inside square brackets.

Each provider (OpenAI, Anthropic, Google and others) has its own address and slightly different field names, but the shape is the same. Providers also offer SDKs, ready-made code libraries that build the request for you.

No-code tools do exactly this behind the scenes. When you connect an OpenAI, Claude or Gemini account in Zapier or Gumloop by pasting an API key, every AI step becomes an API request, and the fields you fill in become parts of it.

Example

REQUEST (simplified; field names differ a little between providers)
{
  "model": "<model name from the provider's docs>",
  "max_output_tokens": 300,
  "messages": [
    {"role": "system", "content": "Reply in one short sentence."},
    {"role": "user",   "content": "What is a haiku?"}
  ]
}

RESPONSE (simplified)
{
  "text": "A haiku is a short Japanese poem of three lines.",
  "stop_reason": "completed",
  "usage": {"input_tokens": 21, "output_tokens": 12}
}

Remember

  • An API lets programs send a request to a model and get a response
  • The API key is a secret that identifies and bills you; never share it
  • Requests name a model and carry messages; responses carry text, stop reason and token usage

Watch out for

  • Putting an API key directly in a shared file, spreadsheet or code repository; store it in the tool's secure connection or an environment variable.

Check yourself

  1. A colleague pasted the company's API key into a public forum post. What should happen first?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: developers.openai.com/api/docs/quickstart