Hit the Ground Running with FastAPI!

     I recently had a job interview with a company that told was building API's in FastAPI, a Python framework for building high performance, fast to code API's. From the research I did, it sounded like a it achieved a lot of the things Flask seeks to do, but faster and with an even simple way to code them. I started learning some FastAPI and coding along with the tutorial on the website found here, and I was amazed at how fast I could set up a simple API. The code I've included below is an entire API, in less than 25 lines of code. I have two get requests and a post request that even handles some data validation.

    Since my API is so small, you can easily run it on your machine as well provided you have Python 3.6+ running on your machine. Installing FastAPI is as simple as two pip installs (the tutorial has a way to install both at once but I ran into errors until I installed them both separately):

pip install fastapi

pip install uvicorn[standard] 

     Once FastAPI is installed and you've got the code in your file, you can use the following command to start up your API. It will tell you where it's running, and by adding /docs to the end of the path you can test the endpoints with the Swagger interface.

uvicorn main:app --reload

    I can easily see this framework being fantastic for processing data, since Python is such a great language for working with data anyway. I liked how the data validation worked, by importing Query and passing in a few parameters I was able to quickly specify the type of data I didn't want to see, which automatically generated the HTTP response applicable to the scenario. Having spent some time recently doing data validation in C#, I was impressed with FastAPI's simplicity. I will absolutely be continuing to learn FastAPI and hopefully work on and API that tackles a large amount of data soon.

Comments

Popular posts from this blog

API's in C#

Using WebRTC to build a videophone in React and TypeScript

Reviewing WPF and MVVM