Skip to lesson

Reading an API's documentation

A teammate gives you an API you did not write and says "just call it". You need its address, its login, its requests and its error codes first. The examples use PinFinder, a practice API for Indian post office pincodes.

What good docs contain: base URL, auth, endpoints, examples, errors

API documentation is the guide that tells you how to call an API you did not write: where it lives, how to log in, which requests exist, what they return and how they fail.

Find five things in the docs before your first call. The example makes a first call with the base URL and the API key taken from the docs.

Example 1 · A first call with the key from the docs

Terminal
curl -i -H "X-API-Key: $PINFINDER_API_KEY" http://localhost:8090/v1/pincodes/560038curl -s http://localhost:8090/v1/pincodes/560038curl -s -H "X-API-Key: $PINFINDER_API_KEY" http://localhost:8090/v1/pincodes/056003
Sample output (your Date differs)
$ curl -i -H "X-API-Key: $PINFINDER_API_KEY" http://localhost:8090/v1/pincodes/560038
HTTP/1.1 200 OK
X-RateLimit-Limit: 5
X-RateLimit-Remaining: 4
Content-Type: application/json
Content-Length: 92
Date: Fri, 16 Oct 2026 05:13:54 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"pincode":"560038","office":"Indiranagar","district":"Bengaluru Urban","state":"Karnataka"}
$ curl -s http://localhost:8090/v1/pincodes/560038
{"type":"about:blank","title":"Unauthorized","status":401,"detail":"Missing API key. Send it in the X-API-Key header","instance":"/v1/pincodes/560038"}
$ curl -s -H "X-API-Key: $PINFINDER_API_KEY" http://localhost:8090/v1/pincodes/056003
{"type":"about:blank","title":"Bad Request","status":400,"detail":"'056003' is not a valid pincode. Use six digits, not starting with 0","instance":"/v1/pincodes/056003"}

What this code does

  • The docs gave the base URL, the header name X-API-Key and the path rule; the key comes from the environment variable PINFINDER_API_KEY. The answer shows the rate-limit headers too: 4 calls left this minute.
  • The next two calls test the docs' error promises: no key gives 401 with a message naming the header, and a pincode starting with 0 gives 400 with the rule.

Skimming the whole site for an hour is the usual mistake; these five usually sit on the first page or in a "Getting started" section:

  • Base URL: the start of every request, often with a version, such as http://localhost:8090/v1.
  • Authentication: how to prove who you are, such as an API key in the X-API-Key header, a Bearer token, or none.
  • Endpoints: method and path for each operation, such as GET /pincodes/{pincode}, with its parameters and their rules (six digits, first not 0).
  • Examples and errors: a sample request and response, plus every error status with its meaning, so you know what 400, 401, 404 and 429 mean here.
Look for the limits too: calls per minute, the largest page size, and the date a version stops working.

OpenAPI and Swagger UI: reading schemas and trying calls

OpenAPI is a standard format, in JSON or YAML, that describes a whole API: servers, paths, parameters, request and response bodies and security.

Many APIs publish an OpenAPI file, and Swagger UI shows it as clickable docs. The example reads one with jq.

Example 2 · Reading the OpenAPI file with jq

Terminal
curl -s -o openapi.json http://localhost:8090/openapi.jsonjq -r ".info.title, .servers[0].url" openapi.jsonjq ".components.securitySchemes" openapi.jsonjq -r ".paths | keys[]" openapi.jsonjq -r ".paths[\"/pincodes/{pincode}\"].get.responses | to_entries[] | \"\(.key)  \(.value.description)\"" openapi.jsonjq ".components.schemas.Pincode.required" openapi.json
Output
$ curl -s -o openapi.json http://localhost:8090/openapi.json
$ jq -r ".info.title, .servers[0].url" openapi.json
PinFinder API
http://localhost:8090/v1
$ jq ".components.securitySchemes" openapi.json
{
  "ApiKeyAuth": {
    "type": "apiKey",
    "in": "header",
    "name": "X-API-Key"
  }
}
$ jq -r ".paths | keys[]" openapi.json
/pincodes/{pincode}
$ jq -r ".paths[\"/pincodes/{pincode}\"].get.responses | to_entries[] | \"\(.key)  \(.value.description)\"" openapi.json
200  The post office
400  Not a valid pincode
401  Missing or wrong API key
404  No post office has this pincode
429  More than 5 calls in a minute. Wait the number of seconds in Retry-After
$ jq ".components.schemas.Pincode.required" openapi.json
[
  "pincode",
  "office",
  "district",
  "state"
]

What this code does

  • The spec answers the five questions exactly: title and server (base URL), the ApiKeyAuth security scheme (header X-API-Key), the only path, and every response status with its meaning.
  • required in the Pincode schema lists the fields every successful answer must have. These are the fields worth testing.

The file often sits at a path like /openapi.json or /v3/api-docs, and it is the most exact documentation there is: tools generate docs, clients and Postman collections from it. A schema inside it describes the shape of a JSON body: its fields, their types, which ones are required, and examples. Swagger UI is a web page that shows an OpenAPI file as clickable docs, where each endpoint opens to show its parameters, schemas and responses. Authorize (a padlock button) takes your key or token once; Try it out, then Execute, sends a real request and shows the matching curl command, the status and the body.

Treat "Try it out" like any client: it sends real requests. On a production API, a POST or DELETE there changes real data.

Rate limits, 429 and Retry-After

A rate limit caps how many calls a client may make in a time window, such as 5 per minute per API key. Past the limit, the API answers 429 Too Many Requests.

Free plans usually have the lowest limits. The example hits the limit and reads the Retry-After header.

Example 3 · Hitting the rate limit

Terminal
for pin in 560038 600017 411005 110024 682036 560038 600017; do  curl -s -o /dev/null -w "$pin -> %{http_code}  remaining=%header{x-ratelimit-remaining}  retry-after=%header{retry-after}\n" \    -H "X-API-Key: $PINFINDER_API_KEY" http://localhost:8090/v1/pincodes/$pindonecurl -s -H "X-API-Key: $PINFINDER_API_KEY" http://localhost:8090/v1/pincodes/560038
Sample output (the Retry-After seconds depend on timing)
$ for pin in 560038 600017 411005 110024 682036 560038 600017; do
>   curl -s -o /dev/null -w "$pin -> %{http_code}  remaining=%header{x-ratelimit-remaining}  retry-after=%header{retry-after}\n" \
>     -H "X-API-Key: $PINFINDER_API_KEY" http://localhost:8090/v1/pincodes/$pin
> done
560038 -> 200  remaining=4  retry-after=
600017 -> 200  remaining=3  retry-after=
411005 -> 200  remaining=2  retry-after=
110024 -> 200  remaining=1  retry-after=
682036 -> 200  remaining=0  retry-after=
560038 -> 429  remaining=0  retry-after=60
600017 -> 429  remaining=0  retry-after=59
$ curl -s -H "X-API-Key: $PINFINDER_API_KEY" http://localhost:8090/v1/pincodes/560038
{"type":"about:blank","title":"Too Many Requests","status":429,"detail":"Rate limit of 5 calls per minute reached","instance":"/v1/pincodes/560038"}

What this code does

  • %header{name} (curl 7.84 and newer) prints one response header. remaining counts down from 4 to 0, and the sixth call gets 429 with Retry-After: 60.
  • Calls made while blocked are refused too, and the body says why. Waiting the number of seconds in Retry-After is the only fix.

Providers use rate limits to protect their servers and share them fairly. A 429 response often carries a Retry-After header that says how many seconds to wait. Many APIs also send headers such as X-RateLimit-Limit and X-RateLimit-Remaining on every response, so a client can slow down before it hits the wall. The names vary between APIs; the docs list them.

The wrong reaction to 429 is to retry at once in a loop, which keeps you blocked and may get the key banned. Wait for Retry-After (or back off: 1 second, then 2, then 4), and cache answers that do not change, such as a pincode lookup.

Order of execution

Handle a 429

Put the steps in the order a well-behaved client follows.

API keys: getting one and keeping it out of Git

An API key is a long secret string that identifies your app to the API. Anyone who has it can use your quota, and on paid APIs your money.

Keep the key in an environment variable or in a .env file, and keep .env out of Git. The example shows both.

Example 4 · The key in .env, and .env out of Git

Terminal
cat .gitignorecat .env.examplegit status --short --untracked-files=allgit check-ignore -v .envset -a; source .env; set +aecho "${PINFINDER_API_KEY:0:8}..."
Output
$ cat .gitignore
.env
$ cat .env.example
# Copy to .env and put your own key after the = sign
PINFINDER_API_KEY=
$ git status --short --untracked-files=all
?? .env.example
?? .gitignore
?? lookup.sh
$ git check-ignore -v .env
.gitignore:1:.env	.env
$ set -a; source .env; set +a
$ echo "${PINFINDER_API_KEY:0:8}..."
pk_test_...

What this code does

  • git status lists .env.example, .gitignore and lookup.sh, but not .env. git check-ignore -v proves why: line 1 of .gitignore matches it.
  • set -a; source .env; set +a loads the key into the shell. Printing only its first 8 characters checks it without exposing it.

You usually get a key by signing up on the provider's developer site and creating one in its dashboard; many show it only once, so copy it then. Keys come in test and live versions, often marked in the key itself (pk_test_...). The key never goes into code, Postman exports or Git. Commit a .env.example with an empty value so others know which variables to set. To load .env into a terminal, use set -a; source .env; set +a (macOS Terminal and Git Bash). In PowerShell, $env:PINFINDER_API_KEY = "pk_test_..." sets it for the window; to keep it for every new window, use [Environment]::SetEnvironmentVariable("PINFINDER_API_KEY", "pk_test_...", "User") on Windows or an export PINFINDER_API_KEY=... line in ~/.zshrc on macOS.

If a key does reach Git, deleting the file in a new commit is not enough: it stays in the history. Revoke the key in the provider's dashboard, create a new one, then stop tracking the file.

Find the bug

Find the leaked key

Click the line that leaks the key.

This file is committed to Git so others know which variables to set.

Round 1 of 2

Importing an OpenAPI spec into Postman

Import turns an OpenAPI file into a Postman collection: a folder per path, a request per operation, saved example responses and the API's auth already set up.

Click Import, drop in the OpenAPI file or paste its URL, choose Postman Collection and click Import. The example shows what you get.

Example 5 · What an OpenAPI import creates

Terminal
openapi2postmanv2 -s openapi.json -o pinfinder.postman_collection.json -p > /dev/nulljq -r '.info.name, (.item[] | "  " + .name, (.item[] | "    " + .name, (.item[] | "      " + .name)))' pinfinder.postman_collection.jsonjq -r '.. | objects | select(has("request")) | "\(.request.method) \(.request.url.host[0])/\(.request.url.path | join("/"))"' pinfinder.postman_collection.jsonjq -r '.. | objects | select(has("request")) | .response[] | "\(.code) \(.name)"' pinfinder.postman_collection.jsonjq -c '.variable[]' pinfinder.postman_collection.jsonjq -c '.auth.apikey[] | {(.key): .value}' pinfinder.postman_collection.json
Output
$ openapi2postmanv2 -s openapi.json -o pinfinder.postman_collection.json -p > /dev/null
$ jq -r '.info.name, (.item[] | "  " + .name, (.item[] | "    " + .name, (.item[] | "      " + .name)))' pinfinder.postman_collection.json
PinFinder API
  pincodes
    {pincode}
      Find a post office by pincode
$ jq -r '.. | objects | select(has("request")) | "\(.request.method) \(.request.url.host[0])/\(.request.url.path | join("/"))"' pinfinder.postman_collection.json
GET {{baseUrl}}/pincodes/:pincode
$ jq -r '.. | objects | select(has("request")) | .response[] | "\(.code) \(.name)"' pinfinder.postman_collection.json
200 The post office
400 Not a valid pincode
401 Missing or wrong API key
404 No post office has this pincode
429 More than 5 calls in a minute. Wait the number of seconds in Retry-After
$ jq -c '.variable[]' pinfinder.postman_collection.json
{"key":"baseUrl","value":"http://localhost:8090/v1"}
$ jq -c '.auth.apikey[] | {(.key): .value}' pinfinder.postman_collection.json
{"key":"X-API-Key"}
{"value":"{{apiKey}}"}
{"in":"header"}

What this code does

  • openapi2postmanv2 is the open-source converter behind Postman's OpenAPI import, run here from the terminal (npm install -g openapi-to-postmanv2) so you can see the result. In the app, Import gives the same collection.
  • One folder per path, one request with :pincode as a path variable, five saved example responses (one per documented status), baseUrl from the spec's server, and API-key auth that reads {{apiKey}}. Set apiKey in a secret environment variable before sending.

You also get a {{baseUrl}} collection variable. The import saves typing and copying mistakes: every path, parameter and header name comes from the spec. What is left for you is the values: put the API key into an environment variable (here {{apiKey}}), set real parameter values, and add tests.

The import is a starting point, not a test suite. Generated parameter values are examples or random strings that fit the schema, so check each request before you send it.

Next step

Check your understanding

12 quick questions