application programming interface (API)
In programming, an application programming interface (API) is a set of rules and protocols that allows different software applications to communicate with each other.
APIs define the methods and data attributes that developers can use to interact with external software components, operating systems, or microservices.
When you use an API, you’re leveraging predefined functions to send requests and receive responses, enabling your application to perform tasks or retrieve data without needing to know the intricate details of how those tasks are executed internally.
Example
Web APIs are a common example of a concrete type of API. Here’s an example where you use the requests library to interact with a freely available API that provides information about cats:
>>> import requests
>>> response = requests.get("https://catfact.ninja/breeds")
>>> if response.status_code == 200:
... data = response.json()
... print(data["data"][0])
... else:
... print(f"Failed to retrieve data: {response.status_code}")
...
{
'breed': 'Abyssinian',
'country': 'Ethiopia',
'origin': 'Natural/Standard',
'coat': 'Short',
'pattern': 'Ticked'
}
In this example, you send a GET request to an API endpoint and check if the response is successful using a status code. If so, you parse the JSON data returned by the API and print out a portion of it. This is a quick demonstration of how you can interact with a web API to retrieve information.
Related Resources
Tutorial
Python and REST APIs: Interacting With Web Services
Learn to consume web services with the requests library, then explore the tools for building a Python REST API of your own.
For additional information on related topics, take a look at the following resources:
- Interacting With REST APIs and Python (Course)
- Python REST APIs With FastAPI (Course)
- Python REST APIs With Flask, Connexion, and SQLAlchemy – Part 1 (Tutorial)
- Building HTTP APIs With Django REST Framework (Course)
- Working With APIs in Python: Reading Public Data (Course)
- Python's Requests Library (Guide) (Tutorial)
- Python & APIs: A Winning Combo for Reading Public Data (Tutorial)
- Python and REST APIs: Interacting With Web Services (Quiz)
- Making HTTP Requests With Python (Course)
- Python's Requests Library (Quiz)
- Python & APIs: A Winning Combo for Reading Public Data (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.