Getting Started¶
This tutorial will guide you through setting up the 2Sigma Backend from scratch and making your first API request.
What You'll Learn¶
By the end of this tutorial, you will:
- Set up a local PostgreSQL database
- Install and configure the backend
- Run database migrations
- Start the development server
- Create a user and authenticate
- Make your first API request
Prerequisites¶
Before starting, ensure you have:
- Python 3.11 or 3.12 installed
- PostgreSQL 14+ installed and running
- Git installed
- A text editor or IDE
- Basic understanding of REST APIs
Estimated Time
This tutorial takes approximately 30 minutes to complete.
Step 1: Clone the Repository¶
Step 2: Create a Virtual Environment¶
You should see (venv) in your terminal prompt.
Step 3: Install Dependencies¶
This installs FastAPI, SQLAlchemy, Alembic, and all other dependencies.
Step 4: Set Up PostgreSQL Database¶
Create the Database¶
# Connect to PostgreSQL
psql -U postgres
# Create database and user
CREATE DATABASE ai_tutor_dev;
CREATE USER ai_tutor_user WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE ai_tutor_dev TO ai_tutor_user;
\q
Configure Environment Variables¶
Create a .env file in the ai-tutor-backend directory:
# Application
APP_NAME="2Sigma Backend"
DEBUG=true
ENVIRONMENT=development
# Database
DATABASE_URL=postgresql+asyncpg://ai_tutor_user:your_password@localhost:5432/ai_tutor_dev
# Security
SECRET_KEY=your-super-secret-key-change-this-in-production
ACCESS_TOKEN_EXPIRE_MINUTES=10080
REFRESH_TOKEN_EXPIRE_DAYS=30
# CORS
BACKEND_CORS_ORIGINS=["http://localhost:3000", "http://localhost:8000"]
# LLM Configuration
LLM_PROVIDER=anthropic
ANTHROPIC_API_KEY=your-anthropic-api-key
ANTHROPIC_MODEL=claude-3-5-sonnet-20241022
Security
- Generate a strong
SECRET_KEYfor production (useopenssl rand -hex 32) - Never commit
.envfiles to version control - The example tokens expire in 7 days (10080 minutes) for development convenience
Step 5: Run Database Migrations¶
Apply all database migrations to create the schema:
You should see output indicating migrations are being applied:
INFO [alembic.runtime.migration] Running upgrade -> abc123, initial
INFO [alembic.runtime.migration] Running upgrade abc123 -> def456, add chat system
...
Step 6: Start the Development Server¶
You should see:
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: Started reloader process [12345] using WatchFiles
INFO: Started server process [12346]
INFO: Waiting for application startup.
INFO: Application startup complete.
Server Running
Your API is now running at http://localhost:8000
Step 7: Explore the API Documentation¶
Open your browser and navigate to:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
These provide interactive API documentation generated from your code.
Step 8: Register a User¶
Let's create your first user using the API.
Using curl¶
curl -X POST "http://localhost:8000/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "SecurePassword123",
"full_name": "John Doe"
}'
Using the Swagger UI¶
- Go to http://localhost:8000/docs
- Expand
POST /api/v1/auth/register - Click "Try it out"
- Enter the request body:
- Click "Execute"
Expected Response¶
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "bearer"
}
Save Your Tokens
Copy the access_token - you'll need it for authenticated requests.
Step 9: Make an Authenticated Request¶
Now let's fetch your user profile using the access token.
Using curl¶
Replace YOUR_ACCESS_TOKEN with the token from Step 8.
Using Swagger UI¶
- At the top of the Swagger UI, click the Authorize button
- Enter:
Bearer YOUR_ACCESS_TOKEN - Click "Authorize" then "Close"
- Expand
GET /api/v1/users/me - Click "Try it out" then "Execute"
Expected Response¶
{
"id": 1,
"email": "user@example.com",
"full_name": "John Doe",
"is_active": true,
"locale": "en",
"time_zone": "UTC",
"created_at": "2026-01-20T10:30:00Z",
"current_streak_days": 0
}
Step 10: Create Sample Data (Optional)¶
To explore the full system, you may want to create universities, courses, and content. Here's a quick example:
Create a University¶
curl -X POST "http://localhost:8000/api/v1/universities/" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Example University",
"short_code": "EXUNI",
"country_id": 1,
"time_zone": "America/New_York"
}'
Country IDs
You'll need to populate the Country table first, or adjust the schema to make it optional during development.
Troubleshooting¶
Database Connection Errors¶
If you see connection refused errors:
- Verify PostgreSQL is running:
pg_isready - Check your
DATABASE_URLin.env - Ensure the database exists:
psql -l
Migration Errors¶
If migrations fail:
- Check database permissions
- Verify the database is empty (or use
alembic downgrade baseto reset) - Review error messages for specific issues
Import Errors¶
If you get ModuleNotFoundError:
- Ensure your virtual environment is activated
- Reinstall dependencies:
pip install -r requirements.txt
Next Steps¶
Congratulations! You now have a working 2Sigma Backend.
Here's what to explore next:
- Local Development Guide - Learn about the development workflow
- API Reference - Explore all available endpoints
- Architecture Overview - Understand the system design
- Database Schema - Learn about the data model
Common Workflows¶
Daily Development¶
# Activate environment
source venv/bin/activate # or venv\Scripts\activate on Windows
# Start server
uvicorn app.main:app --reload --port 8000
After Pulling Changes¶
Running Tests¶
Need help? Check the How-To Guides or review the Architecture docs.