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
- Check the status code before parsing: 200 OK, 401 bad key, 403 not allowed on your plan, 404 wrong URL, 429 too fast, 5xx their problem.
- Wrap the call in a timeout and a try/except so a slow or failing API doesn’t hang your program.
- On 429, read
Retry-Afterand back off — don’t hammer. - Guard for missing fields (
data.get("x")) — responses aren’t always complete.
6. Be a good citizen
- Cache responses that don’t change often (prices for a minute, country data for a day).
- Stay well under the rate limit; add a small delay between calls in loops.
- Send a descriptive User-Agent when the API asks for one.
- Keep the key server-side — proxy the call through your own backend rather than exposing it in a browser or mobile app.
A minimal checklist
| Auth | Key location confirmed; key in an env var |
| First call | Works with curl before any code |
| Errors | Status checked; timeout set; 429 handled |
| Politeness | Caching where sensible; under the rate limit |
| Secrets | No 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
๐ Explore free public APIs
Hundreds of free APIs, searchable by category, with auth & CORS at a glance.
Browse the directory โ