API Directory โ€บ Guides โ€บ How to Use a Public API: Step by Step

How to Use a Public API: Step by Step

A hands-on, beginner-friendly walkthrough: pick an API, read the docs, make your first request, and handle the response โ€” with copy-paste examples.

This is the end-to-end path from ‘I found an interesting API’ to ‘my code is using it’. It is the same six steps for every public API, whatever the language.

1. Read the authentication section first

Before anything else, find out: does it need a key? If so, where does the key go — a header, a query parameter, an Authorization: Bearer? Is there a separate secret? This one section determines whether your first request returns data or 401.

2. Get a key (if needed)

Sign up, create an ‘application’ or ‘project’ in the dashboard, and copy the key. Note any activation delay — some keys take minutes to hours to start working. Store the key in an environment variable, never in your code.

3. Make one request by hand

Before writing a program, run a single request with curl or your browser. This confirms the URL, the auth and the response shape with nothing else in the way.

curl -s "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd"
# {"bitcoin":{"usd":64213}}

# with a key in a header:
curl -s https://api.example.com/v1/me -H "Authorization: Bearer $KEY"

4. Move it into code

import os, requests

r = requests.get(
    "https://api.example.com/v1/data",
    params={"q": "lagos"},
    headers={"Authorization": f"Bearer {os.environ['API_KEY']}"},
    timeout=10,
)
r.raise_for_status()          # stop here if the status is 4xx/5xx
data = r.json()
print(data)

5. Handle what goes wrong

6. Be a good citizen

A minimal checklist

AuthKey location confirmed; key in an env var
First callWorks with curl before any code
ErrorsStatus checked; timeout set; 429 handled
PolitenessCaching where sensible; under the rate limit
SecretsNo key in git, front-end, or the URL if avoidable

FAQ

Do I always need an API key?

No — many public data APIs (weather, facts, images, some crypto and geo APIs) work with no key at all, usually with a lower IP-based rate limit.

Can I test an API without writing code?

Yes. Paste a GET URL into your browser, use curl, or use a tool like Postman or Hoppscotch. Many API docs also have a ‘Try it’ console.

Where should I keep my API key?

In an environment variable or a secrets manager, read at runtime. Never commit it to version control or ship it in client-side code.

What is the first thing to check when a request fails?

The HTTP status code. It tells you whether the problem is auth (401/403), the URL (404), your rate (429) or the server (5xx) — each has a different fix.

Keep going

Try it on a real API

Browse all 196 free APIs โ†’

๐Ÿ”Œ Explore free public APIs

Hundreds of free APIs, searchable by category, with auth & CORS at a glance.

Browse the directory โ†’