Este es un ejemplo completo de cómo crear una API REST con FastAPI, usar PostgreSQL, autenticación JWT, y desplegar en Render.com.
fastapi_tutorial/
├── app/
│ ├── api/
│ ├── models/
│ ├── schemas/
│ ├── utils/
│ ├── main.py
│ ├── config.py
│ └── db.py
├── tests/
├── .env.dev
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
└── README.md
- FastAPI
- PostgreSQL
- SQLAlchemy
- Docker + Docker Compose
- Pydantic (v2)
- JWT (
python-jose) passlib(hashing de contraseñas)- pytest
git clone https://github.com/dibanez/fastapi_tutorial.git
cd fastapi_tutorialPROJECT_NAME=FastAPI with PostgreSQL
API_VERSION=v1
DATABASE_URL=postgresql://postgres:postgres@db:5433/fastapi_dbdocker-compose up --buildVisita http://localhost:8001/docs para ver la documentación interactiva.
curl -X POST http://localhost:8001/auth/register \
-H "Content-Type: application/json" \
-d '{"username": "alice", "password": "secret123"}'curl -X POST http://localhost:8001/auth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=alice&password=secret123"Respuesta esperada:
{
"access_token": "eyJhbGciOiJIUzI1NiIs...",
"token_type": "bearer"
}curl -X POST http://localhost:8001/items/ \
-H "Authorization: Bearer TU_TOKEN_AQUI" \
-H "Content-Type: application/json" \
-d '{"name": "Laptop", "price": 1299.99, "in_stock": true}'curl -X GET http://localhost:8001/items/ \
-H "Authorization: Bearer TU_TOKEN_AQUI"- Usuario:
postgres - Contraseña:
postgres - Base de datos:
fastapi_db - Servicio en Docker:
db
docker exec -it fastapi_app pytestRender no admite docker-compose, pero puedes:
- Subir tu repo a GitHub
- Crear un servicio web en Render:
- Build command:
pip install -r requirements.txt - Start command:
uvicorn app.main:app --host 0.0.0.0 --port 10000 - Añade tus variables de entorno (como
DATABASE_URL)
- Build command:
- Crear un servicio de PostgreSQL y conectar tu backend
MIT License.