Eric J Ma's Website

FastAPI: Flask-like generator of web APIs

written by Eric J. Ma on 2020-01-07 | tags: software development web data science

I test-drove FastAPI yesterday, and true to it's name, it's fast! And when the developers say fast, they mean it in at least two ways:

  1. It's fast to execute.
  2. It's fast to develop an API.

I'd like to write about the second point.

Prior to FastAPI, my development framework of choice to build a web API would have been Flask (though I will admit, I have only developed web apps, not web APIs before). My familiarity with Flask turned out to be extremely helpful for learning FastAPI: its own API tracks very closely with Flask! Many of the same idioms, including instantiating a FastAPI object and using @app.route decorators with string interpolation in the URIs, carry over. This is the first way that FastAPI makes API development fast: by using existing idioms.

Prior to FastAPI, I did have reservations about developing a web API using Flask because I wasn't sure how to provide sufficient documentation to API users/testers on how to use it. (Have you seen Swagger APIs without example usage documentation? Frustrating!) Turns out, FastAPI parses your docstrings to provide API docs. This means you can provide an example usage inside the routing function, and on the /docs endpoint, all they will render on the API docs!

So in summary, FastAPI is a very productive tool to use for developing APIs. I highly recommend you check it out once you hit the appropriate opportunity!

I send out a newsletter with tips and tools for data scientists. Come check it out at Substack.

If you would like to sponsor the coffee that goes into making my posts, please consider GitHub Sponsors!

Finally, I do free 30-minute GenAI strategy calls for organizations who are seeking guidance on how to best leverage this technology. Consider booking a call on Calendly if you're interested!