Build APIs with Falcon in Python — All Essentials You Need
Falcon is a WSGI library for building web APIs using the REST architectural style. Before we get started, let’s get familiar with two important terminologies, which can be mentioned in interviews and the Falcon documentation.
- WSGI — Web Server Gateway Interface. It is an interface specification by which web server and application communicate. A WSGI application is just a callable with a well-defined signature so that you can host the application with any web server that understands the WSGI protocol.
- REST — REpresentational State Transfer. It is a software architectural style that was created to guide the design and development of the architecture for the World Wide Web. Any web service that obeys the REST constraints is informally described as RESTful.
This article covers the traditional synchronous flavor of Falcon using the WSGI protocol. Since this is a practice course for coding rather than a theoretical one, we won’t introduce any more abstract concepts. Now let’s get our hands dirty and start writing our first API with Falcon.
Before we get started, we need to install Falcon and other libraries needed for development. It’s better to install all the libraries specific to a project in a virtual environment so that they can be better maintained and won’t impact global system libraries. Besides, with a virtual environment, you can easily dockerize it and make it workable on different platforms. We will use the
venv package of Python 3 to create the virtual environment.
Gunicorn will be used to host our App. If you are working on Windows, you can use Waitress as an alternative. However, I highly recommend using Linux or macOS to develop APIs because it is much more natural and convenient for testing and debugging in a Linux environment.
HTTPie is a user-friendly tool for making API requests. You can also use cURL but the…