Create Subscription with Stripe API

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

# 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 subscription API. It includes a backend service set up using FastAPI and is compatible with any price point established through the Stripe API. The backend service creates a Stripe subscription and allows all CORS. It also logs sent requests and subscription statuses. The price ID, customer ID, and payment method are fetched during the request from the user. After adding the Stripe API key, the backend service can be activated by clicking the test button. The required environment secrets for this app are STRIPE_SECRET_KEY.

Introduction to the Create Subscription with Stripe API Template

Welcome to the Create Subscription with Stripe API template! This template is designed to help you integrate a custom Stripe subscription API into your software applications with ease. It includes a backend service set up using FastAPI, which is compatible with any price point established through the Stripe API. The backend service is responsible for creating a Stripe subscription and is configured to allow all CORS. It also logs requests and subscription statuses. The template is perfect for builders who want to add subscription functionality to their applications without delving into the complexities of backend development.

Getting Started

To begin using this template, simply 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, paste, or delete any code.

Initial Setup

Before testing the app, you'll need to set up an environment secret for the Stripe secret key:

  1. Go to the Environment Secrets tab within the Lazy Builder.
  2. Create a new secret with the key STRIPE_SECRET_KEY.
  3. Enter your Stripe secret key as the value. You can find this key in your Stripe dashboard under Developers > API keys.

Test: Pressing the Test Button

Once you have set up the environment secret, press the "Test" button. This will begin the deployment of the app and launch the Lazy CLI. The CLI will handle the deployment process, and you won't need to install any libraries or set up your environment.

Using the App

After pressing the "Test" button, Lazy will provide you with a dedicated server link. This link is where you can interact with the API to create Stripe subscriptions. Additionally, Lazy will provide a link to the FastAPI documentation, which you can use to explore the API endpoints and their specifications.

Integrating the App

To integrate the backend service with your front-end application, follow these steps:

  1. Insert the provided front-end integration script into your HTML page.
  2. Replace "PUBLISHABLE STRIPE API KEY" with your actual publishable API key from Stripe.
  3. Replace "YOUR SERVER LINK" with the server link provided by Lazy after pressing the "Test" button.
  4. Replace "PRICE_ID", "CUSTOMER_ID", and "PAYMENT_METHOD_ID" with the actual IDs you want to use for the transaction.
  5. Call the create_subscription() function to initiate the creation of a Stripe subscription.

Here is the sample front-end integration script with placeholders for your specific values:

<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 create_subscription() {
        const months = 1;
        const utmParams = getUTMParameters();

        const response = await fetch("YOUR SERVER LINK/create-subscription", {
            method: "POST",
            headers: {
                'Content-Type': 'application/json'
            body: JSON.stringify({
                price: 'PRICE_ID',
                customer_id: 'CUSTOMER_ID',
                payment_method: 'PAYMENT_METHOD_ID',

        const { subscriptionId } = await response.json();
        return subscriptionId;

By following these steps, you can seamlessly integrate the Create Subscription with Stripe API template into your application and start creating subscriptions for your customers.

Last published
June 15, 2024

More templates like this

Login and Registration

This powerful app skeleton is a good starting place for apps that require login and registration

Fast API


The Get Lazy application Launcher is used to launch and monitor your apps in a production environment - For paid subscriptions only.

Fast API

Backend Server

This skeleton is streamlined for creating backend services using FastAPI. It's an excellent choice for building microservices or APIs with minimal frontend requirements.

Fast API
Create Subscription with Stripe API