A

API

The way programs talk to a service. For AI, it means sending text to a model over the internet and getting text back.

6 chapters11 tutorials

01 · Introduction

What an API is

An API (application programming interface) is a door a service opens for programs. A person uses a website; a program uses the API behind it.

For AI models the idea is simple: you send a request that says here is my instruction and my text, the service runs the model, and you get the answer back as data. Every chat app, every workflow tool and every agent uses an API like this underneath.

Four words you will meet:

  • Endpoint: the address you send to, such as /v1/messages.
  • API key: a long secret string that identifies and bills your account.
  • Request / response: what you send and what comes back, usually in JSON.
  • Rate limit: how many calls you may make per minute.

02 · When to use it

When you need the API

  • A chat app is enough for one-off questions. Use the API when the same task runs many times or on a schedule.
  • You want to connect a model to your own data or product.
  • You need structured answers (JSON) a program can read, not prose.
  • Workflow tools such as Zapier, Make and n8n call the API for you; you only need the key.

03 · Setup

Your first call

  1. Create an account with a provider and add a small amount of credit.
  2. Create an API key. Copy it once and store it in a password manager; it is shown only once.
  3. Send a request. With curl on any computer:
curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model":"claude-haiku-5-5","max_tokens":200,"messages":[{"role":"user","content":"Say hello in one line."}]}'
  1. Read the response: the text is inside content, and usage tells you the tokens spent.

In a workflow tool the same thing is a form: paste the key once, pick the model, write the instruction.

04 · How to use it

Day-to-day use

  • Put instructions that never change in the system message and the changing text in the user message.
  • Ask for JSON with a fixed shape when a program reads the answer; both major providers support a schema you define.
  • Set max_tokens so a runaway answer cannot cost a fortune.
  • Keep the key out of code and chat: an environment variable or the tool's secret store.
  • Log every call's token usage; it is the only way to know what a workflow costs.

05 · Pitfalls

Pitfalls

  • A leaked key is money gone. Anyone with it can bill your account. Rotate it if it ever appears in a chat, a screenshot or a repository.
  • Rate limits return an error (HTTP 429). Wait and retry instead of hammering.
  • Errors happen: timeouts, overloaded servers. Build in one retry with a pause, and a fallback.
  • Models are updated and old names retire. Pin a model name and read the deprecation notices.

06 · Resources

Where to go next

Every provider's quickstart takes ten minutes. Do one, then wire the same call into your workflow tool.

My notes

Sign in to keep private notes on this handbook.Sign in