1. Project Setup¶
First, let's create a new directory for our project and set up a virtual environment.
mkdir artanis-blog-api
cd artanis-blog-api
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Next, we'll install Artanis and Uvicorn:
Now, create a new file called main.py and add the following code:
# main.py
from artanis import App
import uvicorn
app = App()
async def root():
return {"message": "Welcome to the Blog API!"}
app.get("/", root)
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
This is the most basic Artanis application. Let's break it down:
- We import the
Appclass fromartanis. - We create an instance of the
Appclass. - We define a handler function
root. - We register the
roothandler for GET requests to the/path usingapp.get(). - We use
uvicornto run the application.
To run the application, use the following command:
Now, if you open your browser to http://127.0.0.1:8000, you should see the message {"message":"Welcome to the Blog API!"}.
In the next section, we'll add more routes to our API.