Introduction to APIs
1. Introduction to APIs
Welcome to this introductory course on APIs in Python. My name is Chris Ramakers; I am an Engineering Manager with over 20 years of professional experience in software development. In this course we will learn all we need to know to start building integrations with Web APIs using Python. Let's get started!2. What is an API?
Let's start with the basics: what is an API? The abbreviation stands for Application Programming Interface. An API defines a set of rules and abilities for two systems to communicate with each other. Through an API, systems can interact with eachother to exchange or manipulate data. As users, we are rarely directly exposed to APIs; we use a User Interface or UI to interact with software applications. But at a basic level, all computer systems are built with APIs, and we use them all the time without knowing. For example, clicking the "send" button will make our email application use an API to tell the email server to send an email to the recipient.3. Web APIs, clients and servers
APIs come in many different styles and flavors, but for the purpose of this course, we will be focusing on Web APIs. Web APIs are used to enable communication between two software applications over a network or the internet. This communication uses the HTTP protocol, the same protocol our browser uses to retrieve webpages from the internet. In practice, this means a client sends a message over the internet to a server; the server, in turn, responds by sending a message back to the client.4. Types of Web APIs
Let's discuss the three most common types of Web APIs. SOAP, takes a very formal approach and is most often used in enterprise applications where robustness and strict protocols are required. REST, the most popular and most common type, is known for its simplicity, scalability, and ease of integration. GraphQL takes a more sophisticated approach, focusing on precise and flexible data retrieval, minimizing data transfer, and optimizing for performance. In this course, we will work with REST APIs, as it is the most common.5. Working with APIs in Python
Let's discuss two well-known Python libraries for integrating Web APIs, urllib and requests. Urllib comes bundled with Python and is a very powerful module, but that goes at the cost of simplicity. Here we are making a request to the music catalog Web API to get a list of music albums. To print the data we requested, we first need to use the urlopen function to send a request, then use the "read()" function on the response object to get the response data. After that, we call the "decode()" function on the response data to extract the raw data, and then we can finally print the data we received from the API. Luckily, the requests package simplifies things a lot. Requests offers many built-in features which urllib needs additional steps or packages for. In this example, we achieve the same result with a lot less code using the `requests.get()` function. Requests takes care of reading and decoding the response; all we need to do is print the text attribute of the response object.6. Let's code!
Let's practice with the requests package!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.