Quit dawdling with slow, outdated approaches and say goodbye to the tedious process of building APIs from scratch!
Embracing Lightning-Speed Development
Introducing the champions of rapid development: Python and FastAPI!
Join forces with this formidable duo and embark on a journey of unmatched speed and efficiency. In just a few minutes, you’ll unlock the magic of creating APIs, broken down into five incredibly straightforward steps.
Step-by-Step Guide to Rapid API Creation
1. Setting Up Your Python3 Environment
With a few taps on your keyboard, lay the foundation of your exciting project.
1 $ mkdir app && cd app
2 $ python3 -m venv venv
3 $ source venv/bin/activate
4
2. Installing the Necessary Packages
Before we begin crafting, let’s arm ourselves with the essential tools.
1 $ pip install fastapi
2 $ pip install "uvicorn[standard]"
3
3. Drafting Your First FastAPI Script
Time to put pen to paper (or, more aptly, fingers to keys). Watch as lines of code morph into an interactive digital interface.
1 from typing import Union
2
3 from fastapi import FastAPI
4
5 app = FastAPI()
6
7
8 @app.get("/")
9 def read_root():
10 return {"say-hello": "Hello Bits Kingdom"}
11
12
13 @app.get("/items/{item_id}")
14 def read_item(item_id: int, q: Union[str, None] = None):
15 return {"item_id": item_id, "q": q}
16
4. Initiating Uvicorn in Your Terminal
Fire up the engines and behold the birth of your application.
1 $ uvicorn main:app --reload
2
3 INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
4 INFO: Started reloader process [28720]
5 INFO: Started server process [28722]
6 INFO: Waiting for application startup.
7 INFO: Application startup complete.
8
5. Testing Your Creation
The moment of truth! Launch your browser and see your creation come to life.
Open your browser at http://127.0.0.1:8000 or http://127.0.0.1:8000/items/5?q=somequery
1 {
2 "say-hello": "Hello Bits Kingdom"
3 }
Craving More Coding Insights?
Can’t get enough of our Bits Tips? Our dev experts are constantly chipping in with tips that help you improve your coding in a snap! Check out these Terminal, Shell, or Console secrets from a super coder!
✍🏾 This article was written by our copywriter, Sofia, in collaboration with our DEV expert, Michael.