In software development and information technology, an API (Application Programming Interface) is a way for programs to communicate and exchange information. This introduction covers the basic idea of APIs and the widely used REST style for web APIs.
What is an API?
An API is an interface through which programs can communicate and exchange data. It can let one program access functions or information provided by another without needing to know all of its internal implementation.
How programs communicate
One program, the client, sends a request to an API. The API processes the request and returns a response from the server. The exchange follows a defined format so that the programs can work together.
Clients, servers and documentation
The client is the program that wants to use information or a function. The server provides access to it. API documentation describes how to make requests, which formats are supported and what information a response may contain.
Requests and responses
A client sends a request for information or an action. The API processes it and returns a response with the requested information or a status. Many APIs operate over a network, so an internet connection is often required.
How APIs work
An API receives a request, processes it and sends a response using a communication protocol such as HTTP. A client needs to know which operations the API supports.
Public and private APIs
A public API may be available to outside developers, subject to its terms and limits. A private API restricts access to approved users or systems. Examples of services offered through APIs include weather data, social-media features, online payments and access to banking information.
Why use an API?
An API can save development time by allowing a team to use an existing function rather than build the same functionality from scratch. For example, a food-delivery app could connect to restaurant menus, delivery services and order-status information so customers can place and track orders.
Common types of APIs
- Operating-system APIs: Let programs use operating-system functions such as file management, memory management or hardware access.
- Remote APIs: Run on a server and are called over a network, such as a web API.
- Library and framework APIs: Provide functions that developers can use through a software library or framework.
- Web APIs: Run on a web server and commonly communicate using HTTP.
HTTP (Hypertext Transfer Protocol) is used for communication between web clients and servers. Web APIs may exchange information in formats such as JSON (JavaScript Object Notation) or XML (Extensible Markup Language). The source article describes JSON as more popular because it is simpler to use and more compact than XML.
What is a REST API?
REST (Representational State Transfer) is an architectural style used to design web APIs. A REST API uses resources, URLs and HTTP methods to structure communication.
- Resource: An item the API can access, such as a user, product or article.
- URL: The address used to identify and request a resource.
- HTTP methods: GET commonly retrieves data, POST creates data, PUT updates data and DELETE removes data.
HTTP status codes
A response can include a status code describing the result: 200 OK (success), 201 Created (created), 400 Bad Request (invalid request), 404 Not Found (resource not found) or 500 Internal Server Error (server error).
How a REST API request works
A client sends an HTTP request to a URL that identifies a resource. The API processes the request and returns a response. For example, a client might send a GET request to /users/123, where 123 is the user ID, and receive the user’s information in JSON.
A restaurant could use REST APIs to connect to online ordering, food delivery and inventory services. APIs may be free or paid; free services can limit the number of requests per day or month, while paid plans may offer different features or limits. A REST API can be used by web, mobile and other applications, subject to the provider’s documentation and terms.
Contact the site for a free financial consultation.
