Getting started overview

Integrating with Stripe Billing involves a sequence of steps, beginning with account setup and API key retrieval. Developers use these keys to authenticate requests to the Stripe API, enabling the creation and management of subscriptions, customers, and products.

Stripe Billing is built on the core Stripe platform, meaning that developers familiar with other Stripe products, such as Stripe Payments, will find the authentication methods and SDK usage consistent. The process generally moves from setting up your development environment to making a foundational API call, such as creating a customer or a subscription.

This guide outlines the initial steps required to make a successful API call with Stripe Billing, focusing on practical implementation for developers aiming to quickly integrate subscription functionality.

Quick reference table

Step What to do Where
1. Sign Up Create a Stripe account Stripe account registration page
2. Get Keys Retrieve API Secret Key (Test & Live) Stripe Dashboard API keys section
3. Install SDK Install a Stripe client library (e.g., stripe-python) Stripe official SDK documentation
4. Configure Client Initialize the SDK with your Secret Key Your application code
5. First Request Make an API call (e.g., create a customer) Your application code / Stripe API customer creation reference

Create an account and get keys

Before making any API requests, a Stripe account is required. This account provides access to the Stripe Dashboard, where API keys are managed and billing operations are monitored.

  1. Sign Up for a Stripe Account: Navigate to the Stripe registration page and follow the prompts to create a new account. This process typically involves providing an email address, setting a password, and confirming the email.

  2. Access the Dashboard: After signing up, you will be directed to the Stripe Dashboard. The Dashboard is the central hub for managing your Stripe account, including viewing payments, managing customers, and accessing developer tools.

  3. Retrieve API Keys: API keys are essential for authenticating requests to the Stripe API. Stripe provides two sets of keys: publishable keys (for client-side operations, not used for Billing directly) and secret keys (for server-side operations, used for Billing API calls). For server-side operations with Stripe Billing, the Secret Key is necessary.

    • From the Dashboard, go to Developers > API keys or directly visit the API keys section.
    • You will see both your "Publishable key" (starts with pk_test_ or pk_live_) and your "Secret key" (starts with sk_test_ or sk_live_).
    • Copy your Secret key. Ensure you are in "Test mode" for initial development, indicated by the toggle in the Dashboard, which will provide test keys. Live keys should only be used in production environments.
  4. Secure Your Secret Key: Your Secret Key grants full access to your Stripe account. Never expose it in client-side code or commit it directly to public version control systems. It should be stored securely and accessed only by your backend server.

Your first request

To demonstrate a basic interaction with Stripe Billing, we will create a new customer. This is a foundational step, as subscriptions in Stripe are always tied to a customer object. This example uses the Python SDK.

1. Install the Stripe SDK

First, install the Stripe Python library using pip:

pip install --upgrade stripe

For other languages, refer to the Stripe official SDK documentation for installation instructions.

2. Configure your API key

Set your Secret Key in your application code. Replace 'sk_test_YOUR_SECRET_KEY' with the actual Secret Key copied from your Stripe Dashboard.

import stripe

stripe.api_key = 'sk_test_YOUR_SECRET_KEY'

3. Create a customer

Now, make your first API call to create a customer. This example creates a customer with an email and a description. A successful response will return a Customer object containing details like the customer's ID.

try:
    customer = stripe.Customer.create(
        email='[email protected]',
        description='My first test customer for billing',
    )
    print(f"Customer created successfully: {customer.id}")
except stripe.error.StripeError as e:
    print(f"Error creating customer: {e}")

4. Create a product and price (prerequisites for subscription)

Before subscribing a customer, you need a Product and an associated Price. A Product represents what you sell (e.g., "Premium Plan"), and a Price defines how much and how often you charge for it (e.g., "$10 per month").

try:
    # Create a Product
    product = stripe.Product.create(
        name='Basic Plan',
    )
    print(f"Product created successfully: {product.id}")

    # Create a Price for the Product (e.g., $15.00 USD monthly)
    price = stripe.Price.create(
        product=product.id,
        unit_amount=1500, # $15.00
        currency='usd',
        recurring={'interval': 'month'},
    )
    print(f"Price created successfully: {price.id}")

except stripe.error.StripeError as e:
    print(f"Error creating product or price: {e}")

5. Create a subscription

With a customer and a price in place, you can now create a subscription. This links the customer to the product/price, initiating recurring billing.

try:
    # Assuming 'customer' and 'price' objects from previous steps
    subscription = stripe.Subscription.create(
        customer=customer.id,
        items=[
            {'price': price.id},
        ],
    )
    print(f"Subscription created successfully: {subscription.id}")
except stripe.error.StripeError as e:
    print(f"Error creating subscription: {e}")

A successful execution of these steps confirms that your Stripe API keys are correctly configured and your development environment can communicate with Stripe's services. For detailed API responses and parameters, consult the Stripe Subscriptions API reference.

Common next steps

After successfully making your first API calls, consider these common next steps for a more complete integration:

  • Handling Webhooks: Implement Stripe webhooks to receive asynchronous notifications about events in your Stripe account (e.g., successful payments, subscription cancellations). This is critical for keeping your application's state synchronized with Stripe.

  • Customer Portal Integration: Provide customers with a self-service option to manage their subscriptions, update payment methods, and view invoices using Stripe's Customer Portal. This reduces administrative overhead.

  • Checkout Integration: Integrate Stripe Checkout to securely collect payment information and initiate subscriptions from your website or application. This pre-built solution handles PCI compliance and optimizes for conversions.

  • Testing with Live Keys: Once your integration is thoroughly tested in test mode, switch to your live API keys and conduct final tests in a production environment. Remember to use real, but small, transactions for live testing.

  • Error Handling and Retries: Implement robust error handling for API calls, including network errors, rate limits, and specific Stripe API errors. Consider retry mechanisms for transient issues, following API best practices for handling errors.

Troubleshooting the first call

Encountering issues during your first API call is common. Here are troubleshooting tips:

  • API Key Errors: Double-check that you are using the correct Secret Key (sk_test_) and that it is not exposed client-side. Ensure there are no typos or leading/trailing spaces. If copying the key, ensure the entire string is captured.

  • Network Issues: Verify your server or development environment has internet connectivity and can reach Stripe's API endpoints. Proxy settings or firewalls can sometimes interfere with API requests.

  • Stripe Dashboard Logs: The Stripe Dashboard's Logs section (under Developers) provides a detailed record of all API requests made to your account, including their status, request parameters, and responses. This is an invaluable tool for debugging.

  • SDK Version: Ensure your Stripe SDK is up-to-date. Outdated SDKs might not support the latest API features or could contain bugs. Use the upgrade command (e.g., pip install --upgrade stripe) relevant to your SDK.

  • Test Mode vs. Live Mode: Confirm that you are operating in the intended mode. Using test keys in live mode or vice-versa will result in authentication errors or unexpected behavior. The Stripe Dashboard toggle clearly indicates the current mode.

  • Invalid Parameters: Review the Stripe API documentation for the specific endpoint you are calling to ensure all required parameters are present and correctly formatted. Error messages often indicate which parameters are missing or invalid.