Typing Stubs for Stripe

3.5.2.20240106 · active · verified Sat Apr 11

types-stripe is a PEP 561 type stub package for the `stripe` library, providing static type annotations for use with type-checking tools like MyPy, Pyright, or PyCharm. The current version aims to provide accurate annotations for `stripe==3.5.*`. Maintained by the Typeshed project, it is automatically released up to once a day via PyPI.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `stripe` library. When `types-stripe` is installed (and `stripe` is an older version without inline types), type checkers will automatically pick up the provided type hints, allowing for static analysis of `stripe` API calls and response objects.

import os
import stripe
from typing import Optional

# Set your Stripe API key (usually from environment variables)
# For demonstration, use a placeholder. In real applications, ensure this is securely managed.
stripe.api_key = os.environ.get("STRIPE_SECRET_KEY", "sk_test_YOUR_SECRET_KEY")

def get_customer_email(customer_id: str) -> Optional[str]:
    """
    Retrieves a Stripe customer's email.
    Type checkers will use types-stripe (or inline stubs if stripe>=7.1.0) to
    validate API calls and response types.
    """
    try:
        # The 'stripe' library methods are typed by types-stripe (if installed and applicable)
        customer = stripe.Customer.retrieve(customer_id)
        # Type checker knows 'customer' is a stripe.Customer object and 'email' is Optional[str]
        print(f"Customer email: {customer.email}")
        return customer.email
    except stripe.error.StripeError as e:
        print(f"Error retrieving customer: {e}")
        return None

if __name__ == "__main__":
    # This example assumes you have a test customer ID.
    # Replace 'cus_example_id' with a real test customer ID if you want to run it.
    # Note: Without a valid key and customer, this will raise a StripeError.
    customer_id_example = "cus_example_id" # Replace with actual test customer ID
    get_customer_email(customer_id_example)

    # Example of creating a Customer (showing parameters are typed)
    try:
        new_customer = stripe.Customer.create(
            description="Test customer for types-stripe example",
            email="test@example.com"
            # metadata={"order_id": "123"} # Other optional typed parameters
        )
        print(f"Created new customer with ID: {new_customer.id}")
    except stripe.error.StripeError as e:
        print(f"Error creating customer: {e}")

view raw JSON →