Skip to main content

Adding a backend

Build server-side APIs with Python, FastAPI, and PostgreSQL.

When you need a backend

  • User accounts - Authentication and authorization
  • Persistent data - Store data in a database
  • Business logic - Complex calculations server-side
  • Third-party APIs - Secure API key handling
  • File uploads - Process and store files

Getting started

From scratch

Build a REST API for a blog platform with:
- User registration and login
- CRUD operations for posts
- Comments on posts
- Categories and tags
- PostgreSQL database

Adding to existing frontend

Add a Python backend to store tasks in a database.
Include user authentication so each user has their own tasks.
Update the frontend to use the new API.

Tech stack

Appifex generates backends using:

  • FastAPI - Modern Python web framework
  • SQLAlchemy - Database ORM
  • Alembic - Database migrations
  • PostgreSQL - Database (via NeonDB)
  • Pydantic - Data validation

Project structure

backend/
├── app/
│ ├── main.py # FastAPI app entry
│ ├── config.py # Settings and env vars
│ ├── database.py # Database connection
│ ├── models/ # SQLAlchemy models
│ │ ├── user.py
│ │ └── post.py
│ ├── routers/ # API endpoints
│ │ ├── auth.py
│ │ ├── users.py
│ │ └── posts.py
│ ├── schemas/ # Pydantic schemas
│ └── services/ # Business logic
├── alembic/ # Database migrations
└── requirements.txt

API documentation

FastAPI auto-generates interactive docs:

  • Swagger UI: your-api.vercel.app/docs
  • ReDoc: your-api.vercel.app/redoc

Database

Automatic provisioning

Appifex creates a PostgreSQL database on NeonDB automatically.

Database schema

Request your data model:

Create these database tables:
- Users: id, email, password_hash, created_at
- Projects: id, name, owner_id (FK to users), created_at
- Tasks: id, title, status, project_id (FK to projects)

Migrations

Database changes are handled automatically via Alembic migrations.

Authentication

Built-in auth

"Add user authentication with:
- Email/password registration
- JWT tokens for sessions
- Password reset via email"

OAuth providers

"Add Google and GitHub OAuth login"

Clerk integration

"Use Clerk for authentication,
store user data in the database"

Common API patterns

REST endpoints

Create REST API endpoints:
- GET /api/posts - List all posts
- GET /api/posts/{id} - Get single post
- POST /api/posts - Create post
- PUT /api/posts/{id} - Update post
- DELETE /api/posts/{id} - Delete post

Pagination

"Add pagination to the posts endpoint.
Support page and per_page query parameters.
Return total count in response."
"Add search to the posts endpoint.
Support filtering by category and date range."

File uploads

"Add file upload endpoint for user avatars.
Store files in S3, save URL in database."

Environment variables

Sensitive data is stored securely:

# Accessed in code via config
DATABASE_URL=postgresql://...
JWT_SECRET=...
STRIPE_API_KEY=...

Connecting frontend to backend

After adding a backend, update frontend calls:

// Before (mock data)
const tasks = mockTasks;

// After (API call)
const tasks = await fetch('/api/tasks').then(r => r.json());

Appifex handles this automatically when you add a backend.

Best practices

Validation

"Validate all input data.
Return clear error messages for invalid requests."

Error handling

"Add proper error handling with:
- 400 for bad requests
- 401 for unauthorized
- 404 for not found
- 500 for server errors"

Security

"Add rate limiting to prevent abuse.
Sanitize inputs to prevent SQL injection."

Example prompts

E-commerce backend:

Build an e-commerce API with:
- Product catalog with categories
- Shopping cart
- Order processing
- Stripe payment integration
- Order history

Social app backend:

Create a social media API with:
- User profiles and follow system
- Posts with likes and comments
- Feed algorithm (following + popular)
- Notifications

SaaS backend:

Build a multi-tenant SaaS API with:
- Organization management
- Team invitations
- Role-based permissions
- Usage tracking and billing