Skip to content

What Is an API? Definition, Contract and Example

Quick answer

A plain definition of an API as a contract between programs, with a request and response example, a Python snippet and how an API differs from a UI.

What Is an API? Definition, Contract and Example

An API (Application Programming Interface) is a set of features and rules that lets one piece of software interact with another, as opposed to a person using a screen. It works like a contract: the application states what you can ask for and what you will get back, and other software follows those terms.

Key takeaways

  • An API is software talking to software; a user interface is software talking to a person.
  • Think of it as a contract: allowed requests in, defined responses out.
  • Most web APIs work over HTTP, so a request and a response are the basic unit.
  • You can call a JSON API from Python in a few lines.

On this page:

TermWho talks to whomExample
APISoftware to software, under agreed rulesA food app asking a payment service to create a payment
Web APIAn API reached over HTTPA weather app fetching a forecast
User interface (UI)Software to a personThe buttons and screens of the app
Library functionCode calling code inside the same programCalling a maths function in your own script

What does API mean?

MDN Web Docs defines an API as a set of features and rules inside a software program that enable interaction with it through software, as opposed to a human user interface. MDN adds that it can be seen as a simple contract between the application offering it and other items, such as third-party software or hardware.

Amazon Web Services describes APIs as mechanisms that enable two software components to communicate, and uses the same contract-of-service idea. A restaurant is a handy picture. The waiter is the API: you never walk into the kitchen. The menu is the contract, since it lists what you may order.

How does an API work?

One program sends a request, and the other sends back a response. AWS illustrates this with weather: a weather bureau's system holds daily weather data, and the weather app on your phone talks to that system through APIs and shows you the daily updates.

Most web APIs ride on HTTP. The standard that defines it, RFC 9110 from the IETF, calls HTTP a stateless application-level protocol. Here is an illustrative food-delivery call with made-up values and no real endpoint. The app sends:

POST /payments
{"order_id": 4521, "amount_inr": 349}

and the payment service replies:

{"status": "created", "payment_id": "p_889"}

In Python, calling a JSON API looks like this. The address api.example.com is a placeholder, so swap in a real public endpoint.

import requests

response = requests.get("https://api.example.com/forecast", params={"city": "Pune"}, timeout=10)
response.raise_for_status()
data = response.json()
print(data)

Where are APIs used?

Anywhere two systems must exchange data. A food-delivery app in India may use one API for maps, another for payments and another for sending SMS. The app does not need to know how those services work inside, only the contract.

Teams also use APIs between their own services. See microservices vs monolith for that design choice, and API development and testing tools for tools to try requests by hand.

What are the common mistakes and confusions?

  • Confusing an API with a server. The API is the contract and entry point, not the machine.
  • Assuming every API is a web API. Libraries and operating systems expose APIs too, with no HTTP involved.
  • Skipping error handling. Always set a timeout and check the response status, as the code above does.
  • Treating a response as always the same. The contract defines the shape, but values change, so read fields by name.

REST is a common style for designing web APIs; our guide to REST API methods and status codes covers it. Other neighbours are endpoint, JSON, HTTP, client and server. Developers who build against APIs also lean on Git commands daily.

Next step

Pick a public JSON API, send one GET request from Python, and print two fields from the response. Then read the REST guide above to see how methods and status codes fit in.

Frequently asked questions

What is an API in simple words?

An API is a set of rules that lets one program ask another program for something and get a defined answer back. It is software talking to software, instead of a person clicking a screen.

What is the difference between an API and a web API?

An API is any interface that lets software interact with an application. A web API is one reached over HTTP, such as a weather service. Libraries and operating systems also offer APIs without any network.

What is a REST API?

REST is a common style for building web APIs on top of HTTP, using methods like GET and POST and status codes in responses. Our REST guide on this blog covers the details.

How does a mobile app use an API?

The app sends a request to a server through the API, receives data in the response and shows it on screen. A weather app fetching a forecast is the standard example.

What is an API contract?

It is the agreed description of what requests an API accepts and what responses it returns. MDN describes an API as a simple contract between the application offering it and other software.

Share LinkedIn X WhatsApp
×

Our Courses

Practice-Based Learning Tracks, Supercharged By A.I.