What Is an API?
Learn what an API is, how one program asks another for data or actions, and what a real web request and response look like.
Key takeaways
- An API is a contract that says what you can ask a program to do, what you must send, and what you get back.
- Most APIs people talk about today are web APIs, which use HTTP requests and usually exchange data as JSON.
- The caller never touches the provider's database or code directly, so the provider can change its insides without breaking callers.
- Access is usually controlled with keys or tokens, and limited so one client can't overwhelm the service.
- Good documentation and a stable version scheme matter as much as the API itself.
An API, short for application programming interface, is a defined way for one piece of software to ask another to do something. It lists the requests you're allowed to make, the information each one needs, and what comes back. A weather app that shows tomorrow's forecast is almost certainly getting it through an API.
The point is that the program asking doesn't need to know how the other side works inside. It only needs to follow the published rules. If you understand that idea, you can read most conversations about integrations, developer platforms and "connecting" one service to another.
The idea behind the word "interface"
An interface is the surface where two things meet. A wall socket is a good picture: the shape of the holes and the voltage are fixed, so any appliance that matches them works, and the appliance doesn't care how the power station produces the electricity.
Software works the same way. A program that exposes an API says, in effect, "send me these inputs in this shape and I'll return this output." Everything behind that promise, such as the database tables, the programming language and the servers, stays private.
This separation is what lets large systems be built by many teams. The team that handles payments can rewrite its whole internals over a weekend, and as long as the interface behaves the same way, the checkout page never notices.
APIs are everywhere, not only on the web
The term is older than the web and covers several kinds of interface.
| Kind of API | Who calls it | Example of a request |
|---|---|---|
| Library API | Your own program calling reusable code | "Sort this list" |
| Operating system API | Applications asking the OS | "Open this file for reading" |
| Browser API | Scripts on a web page | "Ask the user for their location" |
| Web API | Programs over a network | "Give me order number 1042" |
When someone says "does it have an API?" about an online service, they nearly always mean the last row: can another program reach it over the internet and get structured answers back, without a person clicking through its website.
How a web API call works
A web API follows the same client and server model as the rest of the web. The client sends a request, the server processes it and sends a response. The transport is HTTP, with its requests, responses and headers.
Say a fictional bookshop offers an API. To look up one book, your program might send this:
GET /v1/books/9001 HTTP/1.1
Host: api.example.com
Accept: application/json
Authorization: Bearer YOUR_TOKEN The server might answer like this:
HTTP/1.1 200 OK
Content-Type: application/json
{"id": 9001, "title": "A Field Guide to Cables", "in_stock": true} Each line has a job. GET says you want to read something. The path names the thing. The headers describe the format you accept and prove who you are. The status code, 200 OK, says it worked, and the body carries the data.
The app sees only the middle server. The database stays private.
Endpoints, methods and parameters
An endpoint is a specific address that accepts requests, such as /v1/books. The method is the verb: GET reads, POST creates, PUT and PATCH change, and DELETE removes. The article on HTTP methods explains the differences, which matter more than they first appear.
Parameters are the details of a request. They can sit in the path (/books/9001), in the query string after a question mark (/books?author=lee&limit=10), in headers, or in the request body. The documentation tells you which goes where.
Many web APIs follow a loose style called REST, covered in REST APIs explained. Others use GraphQL, where the client writes a query naming exactly the fields it wants, or remote procedure call styles such as gRPC and JSON-RPC, where a request names a function to run.
What comes back
Most web APIs answer with JSON, a text format built from names, values and lists. Some older or industry-specific ones use XML, and high-volume internal systems sometimes use compact binary formats.
The status code tells you the category of outcome before you read any data:
- Codes in the 200s mean the request succeeded.
- Codes in the 400s mean something is wrong with the request, such as a missing field, a bad token or too many calls.
- Codes in the 500s mean the server failed while handling it.
A well-designed API also returns an error body that says what went wrong in words, often with a machine-readable error code your program can check.
Why services offer APIs
APIs let other people build on a service without anyone copying data by hand. An online store can send each order to its accounting software. A calendar can add meetings created by a booking tool. A mobile app and a website can share one back end because both call the same interface.
For the provider, an API is also a product with rules. It decides which operations exist, who may use them and how often. That control is the reason an API is safer than handing a partner direct access to a database.
Many organizations put a shared front door in front of their APIs to handle authentication, logging and traffic limits in one place. That component is called an API gateway.
Access, limits and safety
Public data might need no sign-in at all, but most APIs identify callers. An API key is a long random string that names your application. A token, often issued through OAuth, can represent a specific user who granted your app limited permission. See API keys, tokens and OAuth for how they differ.
Treat these like passwords. Keep them out of code you publish and out of web pages anyone can view, and revoke them if they leak.
Providers also cap request volume. If you send too many calls, you may get a 429 Too Many Requests response, and a well-behaved program waits before trying again.
What good documentation tells you
An API is only usable if you can find out how it behaves. The documentation is the readable form of the contract, and a solid one answers a predictable set of questions:
- What is the base address, and is there a separate test environment?
- How do you authenticate, and how do you get credentials?
- Which endpoints exist, with which required and optional parameters?
- What does a successful response look like, field by field?
- Which errors can occur, and which of them are worth retrying?
- How many requests may you send, and over what period?
Many providers also publish a machine-readable description, often in the OpenAPI format, from which tools can generate reference pages or client code. Some offer a software development kit, or SDK, which is a library that wraps the raw requests in functions for your programming language. An SDK saves typing, but the requests underneath are the same ones you could send yourself, which helps when something misbehaves. Reading API documentation describes a method for working through an unfamiliar one.
A small scenario from start to finish
Say you run a club website and want it to list upcoming events from the calendar your members already edit. Without an API, someone retypes each event. With one, a small script on your server asks the calendar service for events in the next month, receives a list, and builds the page from it.
You'd make a few decisions along the way. The script needs a credential that can read that calendar and nothing else. It should store the result for a while instead of asking on every page view. And it needs a plan for failure: if the calendar service is slow or down, your page can show the last saved list with a note, not a blank space or an error.
Those three habits, narrow access, sensible caching and graceful failure, apply to nearly every integration, large or small. They also explain why an integration that worked in a demonstration can still cause trouble later: the demonstration only showed the successful call.
Contracts change, so APIs are versioned
Other people's programs depend on an API's exact behavior. Renaming a field or changing a date format can break them silently. Providers therefore add new things without removing old ones where they can, and publish a new version when a breaking change is unavoidable. You'll often see the version in the path, as in /v1/, or in a header.
If your business relies on someone else's API, you also take on their decisions. Pricing, limits or terms can change, and a service can shut down an endpoint. Before building something important on one, read the deprecation policy, check how you could export your data, and keep the calls in one part of your code so replacing them is manageable.
Do you need to care?
If you never write code, you still meet APIs when a tool offers to "connect your account" or an automation service links two apps. Knowing that this means one program gets permission to act through another's interface helps you decide what to approve.
If you do want to try one, a command line tool is enough. The guide to using curl shows how to send a request and read the full response, and a JSON formatter makes the reply easier to scan. Start with a read-only call against an API whose documentation offers a test mode.
Common questions
Is an API the same as a website?
No. A website returns pages designed for people to look at. A web API returns structured data designed for programs. Many companies run both from the same underlying system.
Is using an API the same as web scraping?
No. Scraping extracts data from pages that weren't designed for it, and it breaks when the layout changes. An API is an interface the provider offers on purpose, with stated rules.
Do I need to be a programmer to use one?
Not always. Automation platforms and spreadsheet add-ons call APIs for you through forms and menus. Writing code gives you more control over errors, filtering and unusual cases.