FastAPI Python Tutorial: Build an Analytics API from Scratch

CodingEntrepreneurs β€’ March 22, 2025
Video Thumbnail
CodingEntrepreneurs Logo

CodingEntrepreneurs

@codingentrepreneurs

About

Learn, Build, and Earn. Coding Entrepreneurs is here to help you learn the skills you need to build the projects you want. Learn by doing everything we do step-by-step. Practical and hands-on. Let's do this. πŸ’½ Code: https://github.com/codingforentrepreneurs πŸš€ Enroll. Learn to code. Launch your project: https://cfe.sh/enroll πŸš€

Video Description

FastAPI Python Tutorial: Build an Analytics API from Scratch Own your own data pipeline and built an Analytics API from scratch in this tutorial. We'll go step-by-step building a production-ready API microservice so you can harness time-series data to analyze traffic of any web application. πŸ•ΉοΈ Key Tech: - Python - FastAPI - SQLModel + SQLAlchemy - TimescaleDB - Docker πŸ”— Links & References - Course Code: https://github.com/codingforentrepreneurs/analytics-api - Sign up for Timescale with my link: https://tsdb.co/justin-api-1 - pip install timescaledb-python: https://github.com/jmitchel3/timescaledb-python - Railway Templates: fastapicontainer.com & jupytercontainer.com - FastAPI https://fastapi.tiangolo.com/ - SQLModel: sqlmodel.tiangolo.com/ πŸ‘‰ Topics Covered: βœ… Development Environment Setup - Install Python 3, create virtual environments, and set up your workspace properly. All from scratch. βœ… FastAPI Fundamentals - Build your first API endpoints in minutes with Python basics. βœ… Containers with Docker & Docker Compose - Create optimized containers for both development and production environments βœ… Data Schemas with Pydantic - ensure valid incoming and outgoing data using the powerful Pydantic for serialization and validation. βœ… Use SQLModel to connect FastAPI to PostgreSQL with type-safe database operations based on Pydantic and SQLAlchemy. βœ… Time Series Optimization - Transform regular postgres tables into Timescale hypertables for efficient time-based queries and support massive data ingestion. βœ… Advanced Data Aggregation - Implement time bucket queries that analyze patterns across different time intervals βœ… Production Deployment - Deploy your API to Railway in minutes with public and private connections βœ… TimescaleDB Cloud Integration - Connect to managed database services optimized for time series workloads and saving time and headache managing production databases βœ… Secure your API and data with private networking βœ… And more Chapters 00:00:00 Welcome 00:03:21 Demo 00:06:45 Tools 00:10:31 Setup Development Environment 00:12:15 Download & Install Python 3 00:15:40 Create a Python Virtual Environment 00:20:24 Install Python Packages 00:27:53 FastAPI Hello World 00:33:42 Docker Desktop & Docker Compose 00:44:04 Production Dockerfile for FastAPI 00:52:47 Build & Run FastAPI Container 00:57:54 Development Mode with Docker Compose 01:11:36 Section Wrap Up 01:14:27 Routing & Data Validation 01:16:51 Our First API Endpoint 01:21:11 FastAPI Routing Module 01:26:03 Verify API Endpoint 01:29:21 Basic Data Types 01:37:10 List Data Types 01:41:41 POST Method to Send our API Data 01:51:47 Incoming Data Validation with Pydantic Schemas 01:57:56 Optional Values with Pydantic 02:04:30 Section Wrap Up 02:06:13 Storing Data with SQLModel 02:07:48 Postgres or TimescaleDB with Docker Compose 02:16:39 Load Environment Variables with Python 02:24:07 Pydantic to SQLModel 02:26:35 First SQL Table with SQLModel 02:33:50 Create Database Tables with FastAPI Lifespan 02:40:44 Database Connection Issues 02:45:59 Store Data using SQLModel Sessions 02:51:48 SQLModel Query for List View 02:57:01 Detail Lookup via SQLModel 03:01:50 Update Data with SQLModel 03:05:30 Adding a Datetime Field 03:10:39 Updated At Timestamp Field 03:14:12 Section Wrap Up 03:15:30 Time Series Data in Postgres 03:17:33 SQLModel to TimescaleModel 03:22:04 Creating Hypertables 03:25:33 Chunks & Retention in Hypertables 03:30:56 Verify Hypertables with PopSQL 03:36:34 SQLModel Queries in Notebooks 03:42:06 Aggregate Data with Time Buckets 03:52:46 Time Bucket Aggregations with FastAPI & Timescale 04:01:08 Web Traffic Data and More Aggregations 04:12:43 Section Wrap Up 04:14:02 Deploy 04:15:04 Add CORS to FastAPI 04:17:17 Fork the Analytics FastAPI Project 04:20:34 First Deploy on Railway 04:22:35 Provision Database on Timescale Cloud & Deploy 04:28:35 Test Data to Production Endpoint 04:30:17 Analytics API with Private Networking 04:41:41 Section Wrap Up 04:43:14 Thank you