00 - Appealing API's
In this module we will discuss what is an API, how we can benefit from them, and how we will be creating one.
Last updated
Was this helpful?
In this module we will discuss what is an API, how we can benefit from them, and how we will be creating one.
Last updated
Was this helpful?
API stands for Application Programming Interface. This can be anything that can be used as a sort of hook that we can use to extend the functionality of the programs we right. This can be tricky, as there are many different types of APIs; however, the most common API is a WebAPI. These are API's that reside on the internet and allow us to connect to them via different urls, or endpoints.
Usage of an API is how we can separate concerns on full, or enterprise level application. But heres a better question, how do we use other languages with Python? Or how do we mix any two languages? We send them information that they can both read. This is where the term backend and frontend come from. We make the API's sole responsibility sending and receiving data, and the front ends job is to send it to the API and display it. This way we can use some flavor of JavaScript or even a random Framework we find online. here's another use that goes under the radar a lot. How do we create multiple applications that use the same data? Let's take Instagram for example. Instagram has an Android app, an IOS app, and a web app that runs on a PC. But when we create an Instagram account, we can access our account from anywhere on any device. This is because they have an API that connects to as many or as little apps as they like. Take the following picture for instance
Because API's don't care who sent them the data, we can connect multiple applications to the API with the use of endpoints. It's one database with multiple options of displaying the data
JSON stands for JavaScript Object Notation. It has become a universally accepted means to transport data. A good majority of programming languages have support for JSON because of its simplicity. When we make a request to an API, we (or a third party library) can bundle it up in JSON and send it to an API's endpoint. Then, the API will send a response. This can be more JSON, a status code, or anything. Here is what a sample response from an API would look like
On line 2, we see what the returned response was. In this case, its response is application/json
. This just means it returned a JSON response
Everything else is known as the body of the response. This contains the data that we can display to the user and/or organize in our applications. Notice that JSON looks a lot like a Python Dictionary. This makes it real easy to get used to because its in a KeyValue format
For Red Badge we will be creating a REST API using Flask and and a PostgreSQL Database. Our API will support full CRUD functionality and a user authentication system to protect resources using JSON Web Tokens. We will talk about REST below. Flask is a Python micro-framework that allows for creating small but scalable applications. We will be using a couple of Flask helper libraries to aid in the development and ease of accessing our database. These libraries will include Flask-Migrate, Flask-Marshmallow, Alembic, and SQLAlchemy. Alembic and SQL-Alchemy will handle the creation and execution of our migrations (so we don't have to write raw SQL). SQLAlchemy will also act as our adapter to our database and store data. Marshmallow is what creates our schema's and defines what our tables will contain. Flask-Migrate gives us command line tools for easy migrations, just like in Django
REST stands for REpresentational State Transfer. It is an architectural style, rather than a protocol, where everything is a Resource. Resources are lightweight. REST defines how applications communicate over HTTP. Applications that use REST are loosely-coupled and transfer information quickly and efficiently. While REST doesn’t define data formats, it’s usually associated with exchanging JSON between a client and a server.
One thing to note with REST is that it is stateless. This means that when we receive data, it isn't going to change until we make a new request. This is why we may have to refresh a browser tab when browsing the web. Its because the API has already forgotten who made the request right after it sends it out. This is the main driver behind making our applications efficient and have significantly less overhead.
With all of this new knowledge, lets start building a simple blog API
There are a few things to take away from this. On line 1, we see the HTTP Response Status Code. Anything 200 to 300 means that it worked. More on these here, .