API Academy
🌐 English
  • 🌐 English
  • 🌐 繁體中文
HomePetstore APIExplore more APIs
HomePetstore APIExplore more APIs
🌐 English
  • 🌐 English
  • 🌐 繁體中文
🌐 English
  • 🌐 English
  • 🌐 繁體中文
  1. Developing APIs
  • Introduction
  • Table of Contents
  • API Academy
    • Get Started
      • What is an API?
      • How Does an API Work?
      • How to Call an API?
      • How to Read an API Documentation?
      • Chapter Summary
      • Get realtime weather
    • API Fundamentals
      • API Funtamentals: Overview
      • Method & Path
      • Parameters
      • Request Body
      • Responses
      • API Specification & OAS
      • Chapter Summary
    • Working with APIs
      • Working with APIs: Overview
      • Making Requests from Spec
      • Environments and Variables
      • Chaining Multiple Endpoints
      • Handling Authentication
      • Handling API Signatures
      • Introduction to Scripts
      • Chapter Summary
    • Mocking APIs
      • Mocking APIs: Overview
      • Smart Mock
      • Mock Expectations
      • Cloud Mock
      • Mock Scripts
      • Chapter Summary
    • Designing APIs
      • Designing APIs: Overview
      • Introduction to API Design
      • Creating Your First API Project
      • Analyzing Requirements and Planning Your API
      • Designing Data Models
      • Designing Endpoints
      • Using Components and Reusability
      • Setting Up Authentication
      • API Design Guidelines
      • Chapter Summary
    • Developing APIs
      • Developing APIs: Overview
      • Setup: Install Your AI Coding Assistant
      • Quick Start: From Spec to Running API in 30 Minutes
      • Understanding the Generated Code
      • Testing Your API with Apidog
      • Deployment: Put Your API Online
      • Chapter Summary
    • Testing APIs
      • Testing APIs: Overview
      • Getting Started: Your First Test Scenario
      • Integration Testing and Data Passing
      • Dynamic Values
      • Assertions and Validations
      • Flow Control: If, For, ForEach
      • Data-Driven Testing
      • Performance Testing
      • Test Reports and Analysis
      • CI/CD Integration
      • Scheduled Tasks and Automation
      • Advanced Testing Strategies
      • Chapter Summary
    • API Documentations
      • API Documentations: Overview
      • Publishing Your First API Doc
      • Customizing Documentation Appearance
      • Interactive Features for Consumers
      • Advanced Publishing Settings
      • Managing API Versions
      • Chapter Summary
    • Advanced API Technologies
      • API Technologies: Overview
      • GraphQL
      • gRPC
      • WebSocket
      • Socket.IO
      • Server-Sent Events (SSE)
      • SOAP
      • Chapter Summary
    • API Lifecycle
      • API Lifecycle: Overview
      • Stages of the API Lifecycle
      • API Governance
      • API Security Best Practices
      • Monitoring and Analytics
      • API Versioning Strategies
      • The Future of APIs
      • Chapter Summary
    • API Security
      • API Security: Overview
      • API Security Fundamentals
      • Authentication vs Authorization
      • Understanding OAuth 2.0 and OpenID Connect
      • JSON Web Tokens (JWT)
      • OWASP API Security Top 10
      • Encryption and HTTPS
      • Chapter Summary
    • API Tools
      • API Tools: Overview
      • The Evolution of API Tools
      • API Clients
      • Command Line Tools (cURL, HTTPie)
      • API Design and Documentation Tools
      • API Mocking Tools
      • API Testing Tools
      • All-in-One API Platforms
      • Chapter Summary
    • API Gateway
      • API Gateway: Overview
      • What is an API Gateway?
      • Key Features of API Gateways
      • API Gateway vs Load Balancer vs Service Mesh
      • Popular API Gateway Solutions
      • The BFF (Backend for Frontend) Pattern
      • Chapter Summary
  • Modern Pet Store
    • Pet
      • Get Pet
      • Update Pet
      • Delete Pet
      • Create Pet
      • List Pets
      • Upload Pet Image
    • User
      • Update User
      • Get User
      • Delete User
      • Login
      • Logout
      • Create User
    • Store
      • List Inventory
      • Create Order
      • Get Order
      • Delete Order
      • Callback Example
      • Pay for an Order
    • Payments
      • Pay Order
    • Chat
      • Create Chat Completion
    • Webhooks
      • Pet Adopted Event
      • New Pet Available Event
  • Schemas
    • Pet
    • Category
    • User
    • ApiResponse
    • OrderPayment
    • Tag
    • Order
    • Links-Order
    • PetCollection
    • Bank Card
    • Bank Account
    • Links
    • Error
HomePetstore APIExplore more APIs
HomePetstore APIExplore more APIs
🌐 English
  • 🌐 English
  • 🌐 繁體中文
🌐 English
  • 🌐 English
  • 🌐 繁體中文
  1. Developing APIs

Deployment: Put Your API Online

Your API works locally. You've tested it thoroughly. Now it's time to deploy it to a real server so it's accessible from anywhere on the internet, not just your computer.
This chapter walks you through deploying your API to Railway, a platform that makes deployment ridiculously easy. You'll go from local development to a live, publicly accessible API in about 15 minutes.

Why Deploy?#

Right now, your API only runs on http://localhost:8000. That's fine for development, but it's not very useful:
It stops when you close your laptop
Nobody else can access it
You can't show it to potential employers or clients
You can't use it in a real application
Once deployed, you get a URL like https://user-api-production.up.railway.app that works 24/7 from anywhere. Your API becomes real.

Before You Deploy: Prepare Your Code#

There are a few changes needed to make your code production-ready.

Switch from SQLite to PostgreSQL#

SQLite is great for local development, but production APIs should use a proper database server. Railway provides PostgreSQL for free.
Update database.py:
Open database.py in Cursor and modify it to support both SQLite (for local dev) and PostgreSQL (for production):
This code reads the database URL from an environment variable. If the variable doesn't exist (like when you're developing locally), it falls back to SQLite.

Move secrets to environment variables#

Your auth.py file has a hardcoded SECRET_KEY. That needs to come from environment variables instead.
Update auth.py:

Add CORS configuration#

Your main.py currently allows CORS from anywhere (allow_origins=["*"]). That's fine for development, but in production you should be more restrictive.
Update main.py:
Now you can set ALLOWED_ORIGINS=https://yourapp.com,https://www.yourapp.com in production to restrict access.

Test locally#

Run your API locally to make sure these changes didn't break anything:
Visit http://localhost:8000/docs and verify everything still works.

Deploy to Railway#

Railway is a modern deployment platform. It's free to start, handles databases automatically, and deployment is incredibly simple.

Create a Railway account#

1.
Go to railway.app
2.
Click Start a New Project
3.
Sign up with GitHub (recommended) or email

Install Railway CLI#

The Railway CLI makes deployment easy. Install it:
On Mac:
On Windows:
On Linux:
Or use the standalone installer from docs.railway.app/develop/cli

Login to Railway#

This opens a browser window to authenticate. Once done, you're logged in from the CLI.

Initialize your project#

In your project directory:
Railway asks: "Create a new project or link to an existing one?"
Choose Create new project
Give it a name like "user-api"
Railway creates a project and links your local directory to it.

Add PostgreSQL database#

Choose PostgreSQL from the list. Railway provisions a PostgreSQL database and automatically creates a DATABASE_URL environment variable with the connection string.

Set environment variables#

To generate a secure secret key, you can use:
Copy the output and use it as your SECRET_KEY.
If you want to set CORS origins:
For now, you can leave it as * or just not set it.

Create a Procfile#

Railway needs to know how to start your app. Create a file named Procfile (no extension) in your project root:
web: uvicorn main:app --host 0.0.0.0 --port $PORT
This tells Railway to run your FastAPI app and bind it to the port Railway provides.

Create requirements.txt (if you modified it)#

Make sure your requirements.txt includes all dependencies:
fastapi
uvicorn[standard]
sqlalchemy
psycopg2-binary
python-jose[cryptography]
passlib[bcrypt]
pydantic[email]
python-multipart
Note: psycopg2-binary is needed for PostgreSQL support.

Deploy#

Now deploy your code:
Railway:
1.
Uploads your code
2.
Installs dependencies from requirements.txt
3.
Creates database tables automatically (because of Base.metadata.create_all() in main.py)
4.
Starts your API
This takes about 1-2 minutes.

Get your deployment URL#

Railway shows your deployment URL, something like:
https://user-api-production.up.railway.app
Visit that URL + /docs to see your API documentation live:
https://user-api-production.up.railway.app/docs
Your API is now online!

Test Your Deployed API#

Remember all those test cases you generated with AI in the previous chapter? Now you can use them to test your production API. You don't need to manually test again - just switch environments.

Create a Production environment in Apidog#

1.
Open Apidog
2.
Go to Environments
3.
+ New Environment
4.
Name: "Production"
5.
Base URL: https://your-api-url.up.railway.app (your actual Railway URL)
6.
Save

Run your AI-generated tests#

1.
Switch to the Production environment (dropdown in top right)
2.
Go to any endpoint's Test Cases tab
3.
You'll see all the test cases you accepted in the previous chapter
4.
Select all test cases
5.
Click Run Selected
Apidog runs all your tests against the production API. You'll see:
βœ“ Tests that pass (API behaves correctly)
βœ— Tests that fail (something needs fixing)
If all tests pass, congratulations! Your production API works exactly like your local version.
If some tests fail, check:
Are environment variables set correctly? (railway variables to view)
Is the database connected? (railway logs to check)
Any errors in the logs? (railway logs to see)
This is the power of automated tests. You generated them once with AI, and now you can run them against any environment (local, staging, production) just by switching the base URL.

Understanding the Deployment#

Here's what just happened:
Railway received your code and saw it's a Python project (because of requirements.txt). It automatically:
Created a container to run your app
Installed Python and all dependencies
Set up environment variables (DATABASE_URL, SECRET_KEY, etc.)
Connected your app to the PostgreSQL database
Assigned a public URL with HTTPS
Started your app using the Procfile command
Every time you run railway up, it redeploys with your latest code. The database persists between deployments, so your data isn't lost.

Viewing Logs#

To see what's happening on the server:
This shows real-time logs from your deployed app. You'll see:
Startup messages from Uvicorn
Incoming requests
Any errors or exceptions
Database queries (if you enabled logging)
This is invaluable for debugging production issues.

Making Updates#

When you change your code locally:
1.
Test the changes locally
2.
Run your test suite in Apidog (Local environment)
3.
Run railway up to redeploy
4.
Run your test suite again (Production environment)
Railway automatically handles zero-downtime deployments. Your old version keeps running until the new version is ready, then traffic switches over.

Free Tier Limits#

Railway's free tier includes:
$5 of free credit per month
500 hours of usage (plenty for small projects)
1GB database storage
Automatic HTTPS
Custom domains
For a simple API like this, you'll likely stay within the free tier. If you exceed it, Railway pauses your services until next month or you can add a payment method.

Alternative: Deploy to Render#

If you prefer Render (another good platform), the process is similar:
1.
Push your code to GitHub
2.
Go to render.com and sign up
3.
Create a New Web Service
4.
Connect your GitHub repository
5.
Render detects it's Python and auto-configures
6.
Add environment variables in the dashboard
7.
Click Create Web Service
Render also offers a free tier and handles databases similarly. The main difference is Render deploys from GitHub automatically, while Railway can deploy directly from your local machine.

What You've Learned#

You now know how to:
Prepare your code for production (environment variables, database URLs)
Deploy to Railway with the CLI
Add a PostgreSQL database
Set environment variables securely
Get a public URL for your API
View logs and debug production issues
Update your deployed app
Test production using your AI-generated test suite
Your API is no longer just a local project. It's a real, deployed service accessible from anywhere.

Congratulations!#

You've completed the entire workflow:
1.
Designed your API in Apidog
2.
Generated the code using Cursor's AI
3.
Understood how the code works
4.
Tested it with Apidog's AI-generated test cases
5.
Deployed it to production
You now have a real API running online that you can:
Add to your portfolio
Show to potential employers
Use in actual projects
Continue building on
This is a modern, professional development workflow. You've learned to work with AI tools to be productive, while still understanding what's happening under the hood.
Keep building, keep learning, and most importantly - use these skills to create something you're proud of.
Before you go, check out the Chapter Summary for a quick recap of this journey.
Modified atΒ 2025-12-29 10:42:25
Previous
Testing Your API with Apidog
Next
Chapter Summary
Built with