Create Subscription with Stripe FastAPI

Test this app for free
154
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=["*"],
Get full code

Created: | Last Updated:

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:

`

` 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.

Technologies

Streamline Stripe Payments with Lazy AI Templates: Automate Billing, Invoices, Payments and More  Streamline Stripe Payments with Lazy AI Templates: Automate Billing, Invoices, Payments and More
FastAPI Templates and Webhooks FastAPI Templates and Webhooks

Similar templates

FastAPI endpoint for Text Classification using OpenAI GPT 4

This API will classify incoming text items into categories using the Open AI's GPT 4 model. If the model is unsure about the category of a text item, it will respond with an empty string. The categories are parameters that the API endpoint accepts. The GPT 4 model will classify the items on its own with a prompt like this: "Classify the following item {item} into one of these categories {categories}". There is no maximum number of categories a text item can belong to in the multiple categories classification. The API will use the llm_prompt ability to ask the LLM to classify the item and respond with the category. The API will take the LLM's response as is and will not handle situations where the model identifies multiple categories for a text item in the single category classification. If the model is unsure about the category of a text item in the multiple categories classification, it will respond with an empty string for that item. The API will use Python's concurrent.futures module to parallelize the classification of text items. The API will handle timeouts and exceptions by leaving the items unclassified. The API will parse the LLM's response for the multiple categories classification and match it to the list of categories provided in the API parameters. The API will convert the LLM's response and the categories to lowercase before matching them. The API will split the LLM's response on both ':' and ',' to remove the "Category" word from the response. The temperature of the GPT model is set to a minimal value to make the output more deterministic. The API will return all matching categories for a text item in the multiple categories classification. The API will strip any leading or trailing whitespace from the categories in the LLM's response before matching them to the list of categories provided in the API parameters. The API will accept lists as answers from the LLM. If the LLM responds with a string that's formatted like a list, the API will parse it and match it to the list of categories provided in the API parameters.

Icon 1 Icon 1
251

We found some blogs you might like...