DEV Community

loading...

How to create REST API using Falcon

Apcelent
We deliver Excellent Web, Mobile and Distributed Apps!
・2 min read

Falcon is a bare-metal Python web API framework used for building fast app backends and microservices. A bare-metal server is single-tenant physical server completely dedicated for single customer.

As per the official Falcon website

β€œWhen it comes to building HTTP APIs, other frameworks weigh you down with tons of dependencies and unnecessary abstractions. Falcon cuts to the chase with a clean design that embraces HTTP and the REST architectural style.”

Alt text of image

In this article, we will create a rest API for our application using falcon. This application will perform following tasks:

  • Create a note (POST request)
  • Get a note (GET request)
  • Edit a note (PUT request)
  • Delete a note (DELETE request)

We will use SQLAlchemy as our resource provider for the APIs and gunicorn to serve those APIs.

Set up Falcon

Set up and activate a virtual environment using python3. Install Falcon, SQLAlchemy and gunicorn

pip install falcon
pip install SQLAlchemy
pip install gunicorn
Enter fullscreen mode Exit fullscreen mode

or clone the github repository and install the requirements

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Models

Once the package installation is done, we will set up SQLAlchemy as our resource provider in models.py file:

if __name__ == "__main__":
    from sqlalchemy import create_engine

    engine = create_engine(DB_URI)
    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)
Enter fullscreen mode Exit fullscreen mode

After that, we add a Note model class for creating note objects in models.py

class Note(Base):
    __tablename__ = 'notes'
    id      = Column(Integer, primary_key=True)
    title    = Column(String(50))
    description     = Column(String(50))
    created_at     = Column(String(50))
    created_by     = Column(String(50))
    priority     = Column(Integer)
Enter fullscreen mode Exit fullscreen mode

In resources.py file we define Note resources based on Note model.

class NoteCollectionResource(CollectionResource):
    model = Note
    methods = ['GET', 'POST']


class NoteResource(SingleResource):
    model = Note
Enter fullscreen mode Exit fullscreen mode

After defining models and resources, we add API urls in app.py.

db_engine = create_engine('sqlite:///stuff.db')

app = falcon.API(
    middleware=[Middleware()],
)

app.add_route('/notes', NoteCollectionResource(db_engine))
app.add_route('/notes/{id}', NoteResource(db_engine))
Enter fullscreen mode Exit fullscreen mode

Running the app

Create database by executing following in terminal

python models.py
Enter fullscreen mode Exit fullscreen mode

Run the server

gunicorn --reload app:app
Enter fullscreen mode Exit fullscreen mode

We can view list of all requests at http://127.0.0.1:8000/notes.

Using APIs for notes

Open python shell and execute following requests:

requests.post('http://localhost:8000/notes/',
                 headers={'Content-Type': 'application/json'},
                 data=json.dumps({'title': 'sample note five' , 'description': 'sample notes',
                                    'created_at': '2017-08-18 00:00:00', 'created_by': 'apcelent',
                                    'priority': 3}))

requests.put('http://localhost:8000/notes/2',
                headers={'Content-Type': 'application/json'},
                data=json.dumps({'title': 'sample note five' , 'description': 'sample notes edit',
                                    'created_at': '2017-08-18 00:00:00', 'created_by': 'apcelent',
                                    'priority': 3}))

requests.delete('http://localhost:8000/notes/2')
Enter fullscreen mode Exit fullscreen mode

Performance

Benchmark results for Falcon on official website shows that Falcon performs better than some other popular python frameworks with ability to handle much larger requests in lesser time making it fast and reliable for large-scale microservices and backends.

The source code can be found on github

Hope the article was of help!

The article originally appeared on Apcelent Tech Blog.

Discussion (1)

Collapse
bgadrian profile image
Adrian B.G.

I think the framework creators confuses the word "bare-metal" with "vanilla" or other word. I would be surprised if they make it work without python installed and an operating system! to run directly on the hardware.

Also if you want performance and <10ms responses I suggest using a compiled language, just saying ...