by Lazy Sloth
Create Subscription with Stripe 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
# Constants
STRIPE_SECRET_KEY = os.environ['STRIPE_SECRET_KEY']
# Configure Stripe API key
stripe.api_key = STRIPE_SECRET_KEY
# FastAPI app initialization
app = FastAPI()
# CORS configuration
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
Created: | Last Updated:
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:
- Go to the Environment Secrets tab within the Lazy Builder.
- Create a new secret with the key
STRIPE_SECRET_KEY
. - 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:
- Insert the provided front-end integration script into your HTML page.
- Replace
"PUBLISHABLE STRIPE API KEY"
with your actual publishable API key from Stripe. - Replace
"YOUR SERVER LINK"
with the server link provided by Lazy after pressing the "Test" button. - Replace
"PRICE_ID"
,"CUSTOMER_ID"
, and"PAYMENT_METHOD_ID"
with the actual IDs you want to use for the transaction. - 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:
`
` 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.