A simple CRUD API built with Sanic for practicing the basics of building high-performance web APIs in Python. This project includes a modular structure with controllers, models, database seeding, and runs easily with Docker.
Ideal for developers looking to explore Sanic’s asynchronous capabilities, request handling, and best practices for structuring a web API.
✅ Fully asynchronous API with Sanic
✅ CRUD endpoints for a single entity (Item
)
✅ Modular structure with controllers and models
✅ Database migration and seed scripts
✅ Docker & Docker Compose support for easy setup
✅ Ready-to-use environment for experimenting with Sanic
. ├── app/ │ ├── controllers/ # Route handlers (controllers) │ ├── models/ # Database models │ ├── seeds/ # Seed scripts for database population │ └── main.py # Entry point for the Sanic app ├── database/ │ ├── migrate.py # Migration script │ └── seed.py # Seed script ├── Dockerfile # Docker image definition ├── docker-compose.yml # Docker Compose services configuration ├── requirements.txt # Python dependencies └── README.md # Project documentation
git clone https://github.com/guduchango/sanic-python-example.git
cd sanic-python-example
Build the Docker image:
docker-compose build
Start the containers:
docker-compose up
Run database migrations:
docker-compose run web python database/migrate.py
Seed the database with initial data:
docker-compose run web python database/seed.py
Once the app is running on http://localhost:8000, the following CRUD routes are available:
Method | Route | Description |
---|---|---|
GET | / |
Health check/test route |
GET | /items |
List all items |
GET | /items/<item_id> |
Retrieve an item by ID |
POST | /items |
Create a new item |
PUT | /items/<item_id> |
Update an existing item |
DELETE | /items/<item_id> |
Delete an existing item |
List all items
curl -X GET http://localhost:8000/items
Get a specific item
curl -X GET http://localhost:8000/items/1
Create a new item
curl -X POST http://localhost:8000/items -H "Content-Type: application/json" -d '{"name": "New Item", "description": "A new item added to the database"}'
Update an item
curl -X PUT http://localhost:8000/items/1 -H "Content-Type: application/json" -d '{"name": "Updated Item", "description": "Updated description"}'
Delete an item
curl -X DELETE http://localhost:8000/items/1
Once running, your API will be available at:
http://localhost:8000
- Docker
- Docker Compose
MIT License.
Contributions are welcome! Feel free to fork the repo and submit a pull request.