š Quick Overview
- The Goal: Generate a secure payment link using Python.
- The Tech: Stripe API (The industry standard for online payments).
- The Safety: We will use “Test Mode” (No real money involved yet).
- The Reward: You can finally charge users for your bots and scripts.
In this tutorial, you will learn how to integrate the Stripe API with Python to generate secure checkout links, allowing you to monetize your software projects.
Throughout “Freelance February,” we have built some amazing tools. You have a Telegram Bot, a Price Tracker, and a custom ChatGPT Assistant. But there is one glaring problem: you are giving it all away for free.
If you want to transition from a hobbyist to a professional freelancer or SaaS (Software as a Service) founder, you must learn how to accept payments. Handling credit cards yourself is a security nightmare. Instead, professionals use Stripe.
Today, we will write a Python script that asks Stripe to create a unique payment link. You can send this link to your users to charge them $5 for access to your bot.
Step 1: Get Your Stripe “Test” Keys
Stripe is incredibly developer-friendly. They provide a safe “Test Mode” so you can practice processing payments with fake credit card numbers.
- Go to Stripe.com and create a free account.
- On your dashboard, look at the top right corner and toggle “Test mode” to ON.
- Go to the “Developers” tab, then click “API keys”.
- Copy your Secret key. It will look something like
sk_test_51Nx...
Step 2: The Setup
We need the official Stripe library to talk to their servers. Open your terminal:
pip install stripeStep 3: The Code (Generating a Checkout Link)
Create a file named payment_gateway.py. Our script is going to send a request to Stripe saying: “I want to sell a Premium Bot Subscription for $5.00. Please give me a webpage where the user can pay.”
import stripe
# 1. Authenticate with Stripe
# (In production, ALWAYS use os.getenv("STRIPE_KEY")!)
stripe.api_key = "sk_test_YOUR_SECRET_KEY_HERE"
def create_payment_link():
print("Generating secure checkout link...")
try:
# 2. Ask Stripe to create a "Checkout Session"
session = stripe.checkout.Session.create(
payment_method_types=['card'], # Accept credit cards
line_items=[{
'price_data': {
'currency': 'usd',
'product_data': {
'name': 'LogicPy Premium Bot Access',
'description': 'Unlock all AI features for 1 month.',
},
# Stripe uses cents! $5.00 = 500 cents
'unit_amount': 500,
},
'quantity': 1,
}],
mode='payment', # One-time payment (Use 'subscription' for recurring)
# Where Stripe sends the user after they pay (or cancel)
success_url='https://www.logicpy.com/success',
cancel_url='https://www.logicpy.com/cancel',
)
# 3. Print the URL
print("\nā
Link Generated Successfully!")
print(f"Send this to your customer:\n{session.url}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
create_payment_link()Step 4: Testing the Payment
Run your script. It will print a long URL. Click it!
You will be taken to a beautiful, secure Stripe checkout page featuring your product name and price. Because you are in Test Mode, you can use Stripe’s fake credit card to test it:
- Card Number:
4242 4242 4242 4242 - Expiration: Any date in the future (e.g.,
12/28) - CVC: Any 3 digits (e.g.,
123)
How to Use This in the Real World
You can now tie your entire freelance toolkit together. Imagine this workflow:
- A user messages your Telegram Bot asking for a premium AI feature.
- Your bot runs this Stripe code and replies with the
session.url. - The user pays.
- Your bot gives them access!
Conclusion
You have crossed the bridge from coding for fun to coding for business. By integrating Stripe, you have removed the final barrier to launching your own profitable software products.






