FastAPI Payment Gateway Integration: Stripe

Integrating FastAPI with Stripe offers a powerful combination of high-speed API development and robust payment processing capabilities. Here are the necessary details to get started.

1. Setting up stripe account

Before you can integrate Stripe with FastAPI, you need to set up a Stripe account. Here are the basic steps:

  1. Go to the Stripe website ( and sign up for an account.

  2. In your Stripe dashboard, you will find your API keys. You will need these keys to make API requests. Stripe provides both a test mode and live mode. During development, use the test API keys to avoid real transactions.

2. Install the requirements:

We need to install the basic third party packages to start a fastapi app and to accept payment by stripe. Let's install the below packages with pip install -r requirements.txt


Let's keep the secrets in the .env file. This file should never be pushed to version control systems.


Finally, let's have the code to interact with Stripe using FastAPI:

import os
import json
import stripe
from fastapi import FastAPI, responses, Request, HTTPException
from dotenv import load_dotenv
from pathlib import Path

env_path = Path(".") / ".env"

app = FastAPI()
stripe.api_key = os.getenv("STRIPE_SECRET_KEY")

async def create_checkout_session(price: int = 10):
    checkout_session = stripe.checkout.Session.create(
                "price_data": {
                    "currency": "usd",
                    "product_data": {
                        "name": "FastAPI Stripe Checkout",
                    "unit_amount": price * 100,
                "quantity": 1,
            "user_id": 3,
            "email": "",
            "request_id": 1234567890
        success_url=os.getenv("BASE_URL") + "/success/",
        cancel_url=os.getenv("BASE_URL") + "/cancel/",
    return responses.RedirectResponse(checkout_session.url, status_code=303)"/webhook/")
async def stripe_webhook(request: Request):
    payload = await request.body()
    event = None

        event = stripe.Event.construct_from(json.loads(payload), stripe.api_key)
    except ValueError as e:
        print("Invalid payload")
        raise HTTPException(status_code=400, detail="Invalid payload")
    except stripe.error.SignatureVerificationError as e:
        print("Invalid signature")
        raise HTTPException(status_code=400, detail="Invalid signature")
    print("event received is", event)
    if event["type"] == "checkout.session.completed":
        payment = event["data"]["object"]
        amount = payment["amount_total"]
        currency = payment["currency"]
        user_id = payment["metadata"]["user_id"] # get custom user id from metadata
        user_email = payment["customer_details"]["email"]
        user_name = payment["customer_details"]["name"]
        order_id = payment["id"]
        # save to db
        # send email in background task
    return {}

That's it, now you can visit: You can make the payment with the card id: 4242 4242 4242 4242 on any future date and any cvv. This is a test card btw. Once you complete the payment, you would get a webhook call at the webhook endpoint. To get webhook calls in local you can route them through ngrok. Watch the video to see it in action.




Prev: Deploying FastAPI Applications Next: FastAPI Startup & …

Brige the gap between Tutorial hell and Industry. We want to bring in the culture of Clean Code, Test Driven Development.

We know, we might make it hard for you but definitely worth the efforts.



Refund Policy

Follow us on our social media channels to stay updated.

© Copyright 2022-23 Team FastAPITutorial