Implement Custom Stripe Checkout Page in Python & FastAPI

 import os
import logging
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
import stripe
import uvicorn
from flask import Flask, render_template, request

# Constants

# Configure Stripe API key
stripe.api_key = STRIPE_SECRET_KEY

# FastAPI app initialization
app = FastAPI()

# CORS configuration
origins = ["*"]

About this template

This app integrates a custom Stripe checkout page in a Python application. It includes both a backend and a frontend. The backend service is set up using FastAPI and is compatible with any price point established through the Stripe API. The backend service creates a Stripe checkout session and retrieves the status of a checkout session. It also allows all CORS and logs sent requests and checkout session statuses. The price ID is fetched during the request from the user. After adding the Stripe API key and directing the backend service to the price ID, the backend service can be activated by clicking the test button. The frontend code can be integrated into a Python application to create a custom payment page. This method can be used to set up various types of payment pages, including one-time payments and subscriptions. The required environment secrets for this app are STRIPE_SECRET_KEY and YOUR_DOMAIN.

Introduction to the Stripe Integration Template

Welcome to the guide on how to implement Stripe in your Python application using a Lazy template. This template is designed to help you integrate a custom Stripe payment page into your application with ease. It includes a backend service set up using FastAPI, which handles the creation and retrieval of Stripe checkout sessions, and a frontend script that you can embed into your Python application to initiate the payment process.

Getting Started

To begin using this template, click on "Start with this Template" on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy or paste any code manually.

Initial Setup

Before testing the application, you need to set up the required environment secrets:

  • STRIPE_SECRET_KEY: This is your Stripe secret API key, which you can find in your Stripe dashboard under Developers > API keys.
  • YOUR_DOMAIN: This is the domain where your application is hosted. It is used to construct the return URL after a successful payment.

To add these environment secrets, navigate to the Environment Secrets tab within the Lazy Builder and enter the values for each secret.

Test: Pressing the Test Button

Once you have set up the environment secrets, press the "Test" button to deploy the app. Lazy will handle the deployment, and you won't need to worry about installing libraries or setting up your environment.

Entering Input

If the template requires user input through the CLI, you will be prompted to provide it after pressing the "Test" button. Follow the instructions in the Lazy CLI to enter the necessary information.

Using the App

After deployment, Lazy will provide you with a dedicated server link to use the API. If the template uses FastAPI, you will also receive a link to the API documentation. Use these links to interact with your deployed backend service.

Integrating the App

To integrate the Stripe payment functionality into your frontend, you will need to add the provided frontend script to your application. Here's how:

  1. Locate the `` tag in your HTML file.
  2. Insert the provided frontend script just before the closing `` tag.
  3. Replace `"PUBLISHABLE STRIPE API KEY"` with your actual publishable API key from Stripe.
  4. Replace `"LAZY SERVER LINK"` with the endpoint URL of your published app that you received after deployment.
  5. Replace `"PRICE_ID"` with the actual price ID you want to use for the transaction.

Here is the script you need to add to your frontend:

<div id="checkout"></div>
<script src=""></script>
    const stripe = Stripe("PUBLISHABLE STRIPE API KEY");


    function getUTMParameters() {
        const params = new URLSearchParams(;
        return {
            utm_source: params.get('utm_source') || '',
            utm_medium: params.get('utm_medium') || '',
            utm_campaign: params.get('utm_campaign') || '',
            utm_term: params.get('utm_term') || '',
            utm_content: params.get('utm_content') || ''

    async function initialize() {
        const months = 1;
        const utmParams = getUTMParameters();

        const response = await fetch("LAZY SERVER LINK/create-checkout-session", {
            method: "POST",
            headers: {
                'Content-Type': 'application/json'
            body: JSON.stringify({
                price: 'PRICE_ID',

        const { clientSecret } = await response.json();

        const checkout = await stripe.initEmbeddedCheckout({


By following these steps, you will have successfully integrated a Stripe payment page into your Python application using the Lazy template. If you encounter any issues or need further assistance, refer to the Stripe API documentation or reach out to Lazy's customer support.

Last published
June 15, 2024

More templates like this

PDF Data Extraction and Excel Transfer

An app for extracting name, phone number, and email data from PDF files and transferring it to Excel.


GPT-4 Exam Generator

This app uses GPT-4 to generate a PDF of a new exam based on an uploaded PDF of a past exam.


Job Search Website

A customisable one-page job search website with a search bar and a search button that displays UI cards of matching jobs. You need to have a (free) account on to get the API key required for this to work.

React JS
Implement Custom Stripe Checkout Page in Python & FastAPI