|
Programming

What is an API and why every developer needs to understand it

An API is how one program asks another for something and gets an answer back. Once you see the request and the response, most of the mystery disappears.

What is an API and why every developer needs to understand it

The first time I connected to an API, I spent two days convinced the service was broken. I had the URL, I had the key, and I kept getting a 401. The problem was that I put the key in the wrong header. Two days, one line.

Most API confusion is like that. The concept is simple. The details are where people get stuck, and nobody explains the details because they assume you already know them.

One program asking another

Your app needs something it does not have. Maybe it needs to send an SMS, charge a card, check the weather, or get a list of products from another company. It cannot reach into that company's database. So it sends a request to a URL, and the other program sends back an answer.

That is an API. A door with a guard. You do not get to walk through the building. You tell the guard what you want, in the format he accepts, and he brings it back.

The request has four parts, and each one does a job:

  • The address, which is the endpoint you are calling.
  • The method, which says what you want to do.
  • The headers, which carry your key and the format you expect.
  • The body, which is the data you are sending, if any.

The methods in plain words

GET means give me something. It should never change data. When you open a product page, the app is doing a GET.

POST means here is something new, save it. Creating an order, sending a message, registering a user. PUT or PATCH means change this existing thing. DELETE means remove it. That is most of what you will meet. The names map to the CRUD ideas you already know.

If you remember one rule, remember this: GET should be safe to repeat. If refreshing the page charges the customer again, someone used GET where they should have used POST.

The response tells you what happened

Every response has a status code. 200 means fine. 201 means created. 400 means you sent something wrong. 401 means you are not authenticated, usually a missing or bad key. 404 means the address does not exist. 500 means their server broke, and it is not your fault.

The body is usually JSON, which looks like a Python dictionary with quotes. Something like {"name": "Ada", "balance": 2500}. You read the field you need and ignore the rest.

When an integration fails, the status code is the first thing to check. 401 is a key problem. 422 is usually a validation problem. 500 means stop debugging your code and check their status page.

Keys are passwords, treat them like it

An API key identifies your app and often decides how much you pay. If it leaks, someone else can spend your money or read your customers' data.

Never put a key in frontend JavaScript. Anyone can open the browser dev tools and read it. Never commit it to GitHub, even in a private repo, because private repos become public by accident and forks keep history.

Put keys in a .env file, add that file to .gitignore, and load it on the server. If you already pushed a key, rotate it. Deleting the commit is not enough, because the old commit can still be found.

I learned this the hard way with a payment sandbox key in 2020. Nothing was lost, but I have rotated keys out of habit ever since.

Why this matters even if you do frontend

Frontend developers spend their day consuming APIs. The design is approved, the button is built, and now you need real data. If you do not understand requests and responses, you will wait for the backend developer to hand you a perfect endpoint, and you will not know what to do when the response shape changes.

Backend developers spend their day building APIs for other people, including their own frontend. If you cannot design a clear endpoint, every consumer of your work will guess, and guessing creates bugs.

For jobs, it is one of the most common interview topics. "What is the difference between GET and POST" is a warm up question. "How would you design an endpoint to fetch a user's orders" is the real one.

Call one this week

You do not need a paid service. Use a free public API, or build a tiny one yourself. In VS Code, install an extension like Thunder Client, or use Postman, or just run curl in the terminal.

Send a GET request to a public endpoint. Look at the status code. Look at the JSON. Change one parameter and see what changes in the response. Then send a POST request to a test endpoint and watch the response echo your body back. That hour teaches more than three articles.

After that, build a small page that fetches data and displays it. A list of users, a random joke, today's weather for Lagos. Handle the loading state and the error state. Most beginners handle only the happy path, then act surprised when the network fails on a 3G connection in Ikeja.

The part nobody warns you about

APIs change. A field gets renamed, a version is retired, a rate limit appears. Your code worked in January and breaks in June. That is normal, not betrayal. Read the changelog, pin your version where the API allows it, and write your code so a missing field does not crash the whole page.

Also, do not trust the example in the docs to be current. Docs drift. Test the request yourself before you build a feature around it.

What to do this week

Pick one API, send one GET request, and print the status code and one field from the response. Then put the key in a .env file, add the file to .gitignore, and confirm it does not appear in git status. That is the habit that separates a developer from someone who will leak a key.

To see how the server side is built, read how to build your first CRUD app. When your integration returns an error you do not understand, how to debug code without panicking covers how to read it. The Software development for absolute beginners course includes a section on connecting a frontend to a real API.

Dave Partner

Dave teaches digital skills at Braintem and has helped thousands of Nigerians move from watching tutorials to charging for real work. He writes about the parts nobody puts in the course trailer.