Creating a Basic REST API with Falcon and MongoDB

Recently I started working with the Falcon Framework to make an API for a URL shortening site. A friend of mine showed me the framework and I decided to give it a try. It’s a bit of a learning curve from other common frameworks like Django or Flask, but once you get the hang of it, it’s super easy to work with.

In this article I’ll go over the basic setup of a Falcon REST API with MongoDB support. This is the first part to a multipart series on how to build a complete Dart, Falcon, MongoDB webapp.

To start, we will want a file structure like so:

All of our logic will live inside of the folder. Inside this app folder we have a folder which will hold the helper functions and logic for communicating with our database and performing internal functions, the folder which will hold all of our routes, and the folder will hold the logic for setting up our models in our database. The file contains our configuration for the application where we will store our variables for credentials and what not. Make sure you include an file in every folder inside. The top level file will include our base setup and definition of our application. Let’s start with getting the dependencies needed to run our application.

Make sure you have mongoDB installed on your system, or have a database hosted on a server. If you have it hosted on your server then the constant in our folder will be the IP of that address. If you have the database hosted locally for development purposes you will use as the constant.

You can check if you have mongoDB installed by running:

If you see something like:

then you should be set. There will probably be more information about your environment but the first line is all that matters. For this I’ll be using mongoDB version .

To get setup with our python dependencies we will start by creating a virtualenv:

If you are on Unix or Linux you will also need to install a webserver to run the application. You can use or . If you are on Windows you will want to use the module.

To install uwsgi or gunicorn:

To install waitress and hupper for hot reloads:

Waitress doesn’t include a hot-reload option by default like or so you must use in order to have this feature (it makes development a lot easier but it is not necessary).

Now that we have all of our dependencies installed we can start configuring our application. Open up our file inside . Set it up like the following snippet replacing your information:

This is all we need for now. If you end up using middleware or need to configure other options do it inside this file and import it from your other modules.

Now we can setup our core file inside the folder. Set it up like so:

For now this all we need until we add an endpoint in . Now we can setup our basic User model for the website. This is the only model we will be working in this part of the tutorial. To start create a file inside called . You should setup your file like so:

There isn’t much to the model for now but you’ll add to this later. Now we can move onto setting up our basic register endpoint. Open up the folder and add a file named . Inside of this file we will add the logic to setup our handlers and responses. You should also make a file named inside the folder. These files will work together. Setup the file up like so:

Now that we have our resource setup we can open our core file in the folder. You will want to add these lines to the bottom:

Unlike other frameworks you aren’t going to be returning anything. This is all we need for a basic REST API. We can now communicate with our mongoDB database by sending requests to our server.

Now we can start our webapp:

With Gunicorn:

With uWSGI:

With Waitress and Hupper:

Your server should be running on http://localhost:8000/ now. You should try to post something to the /notes/ endpoint which will allow you to add a note.

QA Engineer @ Reggora | iOS and Python | Tweets are my own opinion and do not reflect my employer | @GetSileo @SecuraRepo @psty_io | 19

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store