Perga API
The backend for Perga, a personal workspace for notes, plans, and ideas.
Overview
Perga API uses FastAPI to serve requests from Perga Web and other API clients.
Features
- Daily planning
- Monthly and custom agendas
- Notes management with folders, export and import
- Notes Attachments with local or S3-compatible storage
- Full-text note search
- User authentication with JWT tokens, including Google sign-in
Demo and Documentation
Tech Stack
- Framework: FastAPI 0.135
- Database: PostgreSQL 16 with SQLAlchemy 2.0
- Migrations: Alembic 1.18
- Validation: Pydantic 2.12
- Authentication: JWT (JSON Web Tokens)
- Containerization: Docker & Docker Compose
Requirements
- Python 3.11
- PostgreSQL
- Docker (optional)
Installation (local)
- Clone and enter the repo
git clone https://github.com/getperga/perga-api.git
cd perga-api
- Create and activate a virtual environment
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
- Install dependencies
pip install -r docs/requirements.txt -r docs/requirements-dev.txt
- Configure environment
cp .env.example .env
# then edit .env
Important variables (from .env.example):
SECRET_KEY— any secure random stringPOSTGRES_HOST,POSTGRES_USER,POSTGRES_PASSWORD,POSTGRES_DBSQLALCHEMY_POOL_SIZE,SQLALCHEMY_MAX_OVERFLOW— database connection pool capacitySQLALCHEMY_POOL_TIMEOUT,SQLALCHEMY_POOL_RECYCLE— connection wait and recycle times in secondsCORS_ORIGINS— JSON array of allowed origins (e.g. ["http://localhost:5173"])IS_SIGNUP_DISABLED— whenTrue, disables the signup endpointGOOGLE_CLIENT_ID— Google OAuth 2.0 Client ID (for "Sign in with Google")GOOGLE_CLIENT_SECRET— Google OAuth 2.0 Client SecretROOT_URL_REDIRECT— optional URL to redirect/to (used by nginx demo)
- Run migrations
alembic upgrade head
- Start the app
uvicorn app.main:app --reload
- Base URL: http://localhost:8000
- Swagger UI: http://localhost:8000/docs
Docker
The Docker image includes PDF and image libraries, without build tools or
package installers. To add a dependency, update docs/requirements.txt and
docs/constraints.txt, then rebuild. The constraints keep dependency versions fixed.
A Docker Compose setup is included. It runs:
db: PostgreSQL 16app: the FastAPI app (exposed on host port 8000)nginx: reverse proxy that serves the app on host port 8080
Start/stop:
docker compose up -d
# ...
docker compose down
Default ports and health:
- App direct: http://localhost:8000 (health:
GET /health/) - Via nginx: http://localhost:8080 (health inside nginx:
GET /health/)
Keep port 8000 on loopback and route public traffic through nginx. Default
per-IP limits are 10 requests/minute for password login and Google code
exchange, and 5/minute for signup. Excess attempts return 429 with
Retry-After: 60. Configure limits and trusted proxies in nginx/nginx.conf.
API Documentation
- Swagger UI: http://localhost:8080/docs (via nginx) or http://localhost:8000/docs (direct)
Application logs go to stdout. For Compose deployments, use
docker compose logs api from the combined installation directory.
API Endpoints (v1)
Authentication
POST /api/v1/auth/signup/— Register a new userPOST /api/v1/auth/access_token/— Get access token (form data)POST /api/v1/auth/access_token_json/— Get access token (JSON)POST /api/v1/auth/refresh_token/— Refresh access token within the current sessionPOST /api/v1/auth/logout/— Revoke the current session using its refresh credentialGET /api/v1/auth/user/— Get current userPUT /api/v1/auth/user/— Update current userPUT /api/v1/auth/user/password/— Change password
Send the access token as Authorization: Bearer …. Send the refresh token in
the JSON body when refreshing or signing out. Refresh tokens have a fixed
30-day lifetime. Signing out ends the current session; password changes and
account deactivation end all sessions. Google sign-in requires a verified
email and identifies linked accounts by Google ID.
Perga Web sends X-Perga-Session: browser and uses an HttpOnly refresh cookie.
Keep Web and API on the same site, and add the exact Web origin to
CORS_ORIGINS. On HTTPS, the cookie also has the Secure flag.
Planner Days
GET /api/v1/planner/days/items/?days=YYYY-MM-DD&days=YYYY-MM-DD— Get items for multiple days; returns a dictionary keyed by date stringPOST /api/v1/planner/days/items/— Create a new day itemPUT /api/v1/planner/days/items/{item_id}/— Update a day itemDELETE /api/v1/planner/days/items/{item_id}/— Delete a day itemPOST /api/v1/planner/days/items/reorder/— Reorder items for a dayPOST /api/v1/planner/days/items/{item_id}/copy/— Copy a day item to another dayPOST /api/v1/planner/days/items/{item_id}/snooze/— Snooze a day item to another day
Agendas (monthly, custom, archived)
GET /api/v1/planner/agendas/?agenda_types=monthly&selected_day=YYYY-MM-DD&with_counts=false— List agendas (filters optional)POST /api/v1/planner/agendas/— Create an agendaPUT /api/v1/planner/agendas/{agenda_id}/— Update an agenda (including archive/unarchive)DELETE /api/v1/planner/agendas/{agenda_id}/— Delete an agendaPOST /api/v1/planner/agendas/reorder/— Reorder agendasPOST /api/v1/planner/agendas/{agenda_id}/action/— Perform an action on agenda (e.g., resolve monthly)
Agenda items:
GET /api/v1/planner/agendas/items/?agenda_ids=1&agenda_ids=2— Get items for multiple agendas; returns a dictionary keyed by agenda idPOST /api/v1/planner/agendas/items/— Create an agenda itemPUT /api/v1/planner/agendas/items/{item_id}/— Update an agenda itemDELETE /api/v1/planner/agendas/items/{item_id}/— Delete an agenda itemPOST /api/v1/planner/agendas/items/reorder/— Reorder items within an agendaPOST /api/v1/planner/agendas/items/{item_id}/copy/— Copy an item (optionally to another agenda)POST /api/v1/planner/agendas/items/{item_id}/move/— Move an item to another agenda
Notes
POST /api/v1/notes/— Create a new noteGET /api/v1/notes/search/?query=...— Search note titles and contentsGET /api/v1/notes/{note_id}/— Get a note by IDPATCH /api/v1/notes/{note_id}/— Update a note (partial), including itsis_pinnedstatePOST /api/v1/notes/{note_id}/append/— Append an HTML fragment to an existing note
Notes Folders
GET /api/v1/notes/folders/— Get all folders for the current userPOST /api/v1/notes/folders/— Create a new folderPATCH /api/v1/notes/folders/{folder_id}/— Update a folderPOST /api/v1/notes/folders/empty-trash/— Permanently delete all notes and folders in the trash
Notes Import & Export
GET /api/v1/notes/export/— Export notes (HTML, Markdown, or PDF)POST /api/v1/notes/import/— Import notes from files (ZIP, HTML, or Markdown)
Notes Attachments
See Notes Attachments for endpoints, storage, limits, and cleanup.
Development
Project Structure
app/commands/— standalone maintenance commands, run withpython -m app.commands.<command>api/— API endpoints and routerscore/— config, DBmodels/— SQLAlchemy modelsschemas/— Pydantic modelsservices/— business logictests/— test suite
Testing
python -m pytest
# or
python -m pytest app/tests/test_services/test_user_service.py
Database Migrations
./scripts/create_migration.sh <migration_name>
# then
alembic upgrade head
License
Released under the MIT License.