Developing a Restful API with FastAPI Python and deploying on Deta Cloud

Posted by


RESTful APIs have become an integral part of modern web development as they provide a standardized way for different applications to communicate with each other over the internet. In this tutorial, we will be using FastAPI, a modern web framework for building APIs with Python, to create a RESTful API. We will also be deploying the API to Deta Cloud, a serverless platform for hosting web applications.

Step 1: Setting up the Environment

Before we start creating our API, we need to set up our development environment. First, make sure you have Python installed on your system. You can download Python from the official website https://www.python.org/. Next, we will install FastAPI and Uvicorn, an ASGI server that will help us serve our API.

You can install FastAPI and Uvicorn using pip by running the following commands in your terminal:

pip install fastapi uvicorn

Step 2: Creating the API

Now that our environment is set up, let’s create our API. Create a new Python file, for example, main.py, and open it in your favorite code editor. In this tutorial, we will create a simple API that returns a list of users.

from fastapi import FastAPI

app = FastAPI()

@app.get("/users")
def get_users():
    return {"users": ["Alice", "Bob", "Charlie"]}

In the code above, we have created a FastAPI instance called app, and a GET endpoint /users that returns a JSON response with a list of dummy users. You can add more endpoints and customize the API to suit your needs.

Step 3: Running the API Locally

To run our API locally, we will use Uvicorn. In your terminal, navigate to the directory where main.py is located and run the following command:

uvicorn main:app --reload

This will start the Uvicorn server, and you should see a message indicating that the server has started. You can now access your API by visiting http://127.0.0.1:8000/users in your browser or using a tool like Postman.

Step 4: Deploying the API to Deta Cloud

Now that we have our API up and running locally, let’s deploy it to Deta Cloud. Deta Cloud is a serverless platform that makes it easy to deploy and scale web applications without worrying about server management.

First, we need to install the Deta CLI, which is a command-line interface for interacting with Deta services. You can install the Deta CLI by running the following command in your terminal:

curl -fsSL https://get.deta.dev/cli.sh | sh

Next, we need to login to our Deta account using the following command:

deta login

Follow the instructions to log in to your Deta account. Once you are logged in, you can deploy your API by running the following command in the same directory as main.py:

deta new

This command will create a new Deta micro called fastapi-api (you can name it whatever you want) and deploy your API to the cloud. You will see a message indicating that the deployment was successful, along with a unique URL where your API is accessible.

That’s it! You have successfully created a RESTful API using FastAPI, and deployed it to Deta Cloud. You can now access your API from anywhere in the world using the provided URL. Feel free to customize and extend your API to suit your specific needs. Happy coding!

0 0 votes
Article Rating

Leave a Reply

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@ngoclamlai
21 days ago

Chất lượng video thấp quá không thấy rõ, thớt chú ý hơn nhé. Video hay!

@baocongtran46
21 days ago

anh k ra video nữa à, video của anh hay lắm

@deta__
21 days ago

This is awesome 👏
Feel free to reach out if you need any help! ☺️

3
0
Would love your thoughts, please comment.x
()
x