For developers

What is an API? A plain explanation with real examples

An API is a set of rules that lets one program ask another for data or actions. Learn how web APIs work, what requests and responses look like, and how to call your first one.

Cursorlane Editorial TeamSoftware & tools 5 min read
A restaurant scene where a waiter carries order slips between diners and an open kitchen

An API (application programming interface) is a defined way for one piece of software to ask another for data or to perform an action, without needing to know how the other side works inside. When a weather widget shows today's forecast or a shop checks your postcode, an API is usually doing the work behind the scenes.

This explainer covers what an API is in plain terms, how web APIs use HTTP, what requests and responses look like, how authentication and rate limits work, and how to make your first API call.

What is an API in plain terms?

A common comparison is a restaurant. You do not walk into the kitchen and cook; you read the menu and give your order to the waiter, who brings back your food. The menu is the API: it lists what you can ask for and how. The kitchen is the system behind it, and you do not need to know how it is organised.

In software, an API is a contract. It says: send a request in this format, to this place, and you will get a response in that format. As long as both sides keep to the contract, either side can change its internals freely. That separation is what lets large systems be built from many independent parts.

The term covers more than the web. A programming language's standard library has an API, an operating system offers APIs for files and networking, and a phone exposes APIs for the camera and location. When people say 'API' in everyday tech talk, though, they usually mean a web API.

How does a web API work?

Most web APIs use HTTP, the same protocol your browser uses to load pages. Your program sends a request to a URL, called an endpoint, such as https://api.example.com/users/42. The server processes it and sends back a response, usually containing data in JSON format, along with a status code.

Many web APIs follow a style called REST, where URLs represent things (users, orders, products) and the HTTP method says what to do with them. Other styles exist, such as GraphQL, where the client describes exactly which fields it wants in a single query, and gRPC, which is common between internal services.

  • GET: read data, for example fetch a user.
  • POST: create something new, for example place an order.
  • PUT or PATCH: update something that exists.
  • DELETE: remove something.
As long as both sides keep to the contract, either side can change its internals freely.

What do requests and responses look like?

A request has a method, a URL, headers and sometimes a body. Headers carry extra information such as the format you want back and your credentials. For a POST request, the body holds the data you are sending, often JSON such as {"name": "Ada", "email": "[email protected]"}.

A response has a status code, headers and usually a body. Status codes are grouped by their first digit, and knowing them makes debugging far easier.

  • 2xx success: 200 OK, 201 Created, 204 No Content.
  • 3xx redirection: the resource has moved.
  • 4xx client errors: 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 429 Too Many Requests.
  • 5xx server errors: 500 Internal Server Error, 503 Service Unavailable.

How do APIs know who you are?

Public data, such as some open government datasets, can be fetched without credentials. Most APIs, though, need to know who is calling, both for security and to apply limits. The simplest method is an API key: a long secret string sent in a header with each request.

When an app acts on behalf of a user, for example to read their calendar, APIs usually use OAuth 2.0. The user signs in with the service and approves specific permissions, and the app receives a token that allows only those actions, without ever seeing the user's password. Tokens often expire and must be refreshed.

Treat keys and tokens like passwords. Keep them out of your code and out of Git repositories, store them in environment variables or a secrets manager, and never put them in code that runs in a user's browser, where anyone can read them.

What are rate limits and API documentation?

Most APIs limit how many requests you can make in a period of time, to protect the service and share capacity fairly. If you go over, you get a 429 status code, often with a header saying when to try again. Well-behaved clients wait and retry with increasing delays, and cache results that do not change often.

Every good API comes with documentation listing its endpoints, parameters, response formats, error codes and limits. Many publish a machine-readable description using the OpenAPI specification, which tools can use to generate interactive documentation and client code. Reading the docs before writing code saves a lot of trial and error.

How do you make your first API call?

You can call a simple API without writing a program. curl, a command-line tool included with most operating systems, sends HTTP requests from a terminal. Running curl followed by an API URL prints the response. Adding -i shows the status code and headers too.

From there, try the same request in code. In Python, the standard library's urllib or a popular HTTP library can fetch the URL and parse the JSON into a dictionary in a few lines. In JavaScript, the built-in fetch function does the same. Start with a free public API that needs no key, such as an open data portal, so you can focus on the request and response before adding authentication.

Frequently asked questions

What does API stand for?

+

Application programming interface. It is the set of rules and formats that one program uses to communicate with another.

What is the difference between an API and a website?

+

A website returns pages designed for people to read in a browser. An API returns structured data, usually JSON, designed for programs to use. Many services offer both, built on the same underlying system.

What is a REST API?

+

A REST API is a web API that follows the REST style: resources are identified by URLs, and standard HTTP methods such as GET, POST, PUT and DELETE act on them. Responses are usually JSON.

What is an API key?

+

An API key is a secret string that identifies the program calling an API. The service uses it to check access and apply limits. Keep it private, like a password.

What is JSON?

+

JSON (JavaScript Object Notation) is a text format for structured data using key-value pairs and lists. It is easy for both people and programs to read, which is why most web APIs use it.

Do I need to know programming to use an API?

+

Not to try one. Tools such as curl or an API testing tool let you send requests by hand. To build something useful with the data, basic programming helps.

Pick a free public API, call it with curl, and read the JSON that comes back. Once you have seen a request and response with your own eyes, the rest of API work is variations on that theme.

How we research: Guides are written from established sources and checked for accuracy. They are general information, not professional advice.

Written by

Cursorlane Editorial Team

Software & tools

Our writers are developers and heavy software users who test what they write about and update guides when the tools change.

More Stories

View all →