Authentication overview
Stripe provides a robust set of authentication mechanisms designed to secure API interactions for processing payments, managing subscriptions, and accessing financial data. The core of Stripe's authentication relies on API keys, which are unique identifiers that grant programmatic access to your Stripe account. These keys are categorized into two main types: secret keys and publishable keys, each serving distinct purposes and having different security considerations. Beyond API keys, Stripe also employs specific methods for securing webhooks, ensuring the integrity and authenticity of event notifications sent from Stripe to your application.
Effective authentication with Stripe involves not only understanding how to generate and use these credentials but also adhering to best practices for their storage, rotation, and revocation. Implementing these measures helps protect sensitive financial data and prevent unauthorized access to your Stripe account. Developers are encouraged to regularly review the official Stripe API keys documentation for the latest guidelines and security recommendations.
The system is designed to integrate seamlessly across various programming languages and platforms, offering client libraries that abstract much of the complexity of secure request signing and handling. This allows developers to focus on application logic while relying on Stripe's infrastructure for secure communication.
Supported authentication methods
Stripe primarily supports API key authentication for direct API requests and a signature-based verification for webhooks. Each method is tailored to specific use cases and security requirements.
API Keys
Stripe distinguishes between two types of API keys:
- Publishable API Keys: These keys are designed to be publicly accessible, often embedded in client-side code (e.g., JavaScript on a checkout page). They are used to create tokens for sensitive payment information, which are then securely sent to your backend. Publishable keys begin with
pk_. They cannot be used to make direct charges or access sensitive data, limiting their impact if compromised. - Secret API Keys: These keys grant extensive access to your Stripe account, allowing you to create charges, manage customers, retrieve detailed transaction information, and perform other administrative actions. Secret keys must be kept strictly confidential and only used on your server-side code. They begin with
sk_and should never be exposed in client-side applications.
Webhook Signing Secrets
When Stripe sends event notifications to your application via webhooks, it includes a unique signature in the HTTP header (Stripe-Signature). This signature is generated using a shared secret specific to each webhook endpoint. Your application must use this secret to verify the authenticity of the webhook event, ensuring it originated from Stripe and has not been tampered with during transit. This process protects against replay attacks and spoofed events. The official Stripe webhook signature verification guide provides detailed steps for implementation.
The following table summarizes Stripe's primary authentication methods:
| Method | When to use | Security Level |
|---|---|---|
| Publishable API Key | Client-side applications (e.g., for tokenizing card data) | Low risk (cannot make charges directly) |
| Secret API Key | Server-side applications (e.g., for creating charges, managing customers) | High risk (full account access if compromised) |
| Webhook Signing Secret | Verifying incoming webhook events from Stripe | High (protects against spoofing and tampering) |
Getting your credentials
All Stripe API keys and webhook signing secrets are managed through the Stripe Dashboard. Accessing and managing these credentials involves a few straightforward steps.
API Keys
- Log in to the Stripe Dashboard: Navigate to the Stripe login page and enter your account credentials.
- Access API Keys: In the Dashboard, go to the Developers section > API keys. You will see both your publishable and secret keys.
- Reveal Secret Keys: Secret keys are hidden by default. Click the "Reveal test key" or "Reveal live key" button to view the full secret key. Keep in mind that live mode keys are distinct from test mode keys.
- Generate New Keys: You can generate new secret keys or publishable keys if needed, for instance, for key rotation or if a key is suspected of being compromised. This functionality is available within the API keys section.
Webhook Signing Secrets
- Access Webhooks: In the Stripe Dashboard, go to the Developers section > Webhooks.
- Select an Endpoint: Choose an existing webhook endpoint or add a new one.
- Retrieve Secret: For each endpoint, a signing secret is displayed. Click to reveal the secret. This secret is unique to each webhook endpoint and should be securely stored in your application's environment variables.
- Generate New Secret: If necessary, you can rotate or generate a new secret for a specific webhook endpoint.
It is crucial to differentiate between test mode keys and live mode keys. Test mode keys (e.g., pk_test_..., sk_test_...) are used for development and testing purposes and interact with Stripe's test environment without affecting real funds. Live mode keys (e.g., pk_live_..., sk_live_...) are for production environments and process actual financial transactions. Always ensure you are using the correct keys for your current environment.
Authenticated request example
When making API requests to Stripe, your secret API key is used to authenticate. This key is passed in the Authorization header of your HTTP request, using the Bearer token scheme. The following example demonstrates a common API request using curl to create a customer, authenticated with a secret key.
curl https://api.stripe.com/v1/customers \
-u sk_live_YOUR_SECRET_KEY: \
-d "name"="Jane Doe" \
-d "email"="[email protected]"
In this example:
https://api.stripe.com/v1/customersis the API endpoint for creating a new customer.-u sk_live_YOUR_SECRET_KEY:is where your secret API key is provided for authentication. The colon after the key indicates an empty password, which is the standard practice for Stripe API key authentication with basic auth incurl.-d "name"="Jane Doe"and-d "email"="[email protected]"are the request body parameters specifying the customer's details.
Alternatively, the key can be passed using the Authorization: Bearer header:
curl https://api.stripe.com/v1/customers \
-H "Authorization: Bearer sk_live_YOUR_SECRET_KEY" \
-d "name"="Jane Doe" \
-d "email"="[email protected]"
Stripe's official client libraries for languages like Python, Ruby, PHP, and Node.js abstract this authentication detail, typically requiring you to set the secret key once in your application configuration. For example, in Python:
import stripe
stripe.api_key = "sk_live_YOUR_SECRET_KEY"
customer = stripe.Customer.create(
name="Jane Doe",
email="[email protected]"
)
print(customer)
For more detailed examples across various languages, consult the Stripe API reference documentation.
Security best practices
To maintain a secure integration with Stripe, follow these essential security best practices:
Protecting API Keys
- Never expose Secret Keys: Your secret API keys should never be committed to version control, exposed in client-side code, or included directly in public repositories. They must be stored and used exclusively on your secure server-side environment.
- Use Environment Variables: Store secret keys as environment variables or in a secure secrets management service. This prevents the keys from being hardcoded into your application's source code. For example, in a Unix-like environment:
export STRIPE_SECRET_KEY="sk_live_YOUR_SECRET_KEY". - Key Rotation: Regularly rotate your API keys, especially secret keys, to minimize the risk associated with a potentially compromised key. Stripe allows you to generate new keys and revoke old ones from the Dashboard.
- Least Privilege: Create separate API keys for different services or applications if possible, and only grant the minimum necessary permissions to each key. While Stripe API keys generally provide broad access, this principle is crucial for other API integrations.
- Restrict Dashboard Access: Limit access to your Stripe Dashboard to authorized personnel only. Implement strong password policies and enable multi-factor authentication (MFA) for all Stripe Dashboard users.
Securing Webhooks
- Verify Webhook Signatures: Always verify the signature of incoming webhook events. This ensures that the event was sent by Stripe and has not been altered. Failure to do so can leave your application vulnerable to spoofed events and potential attacks. The Twilio Webhook Security Guide provides a broader perspective on webhook security principles that apply across different platforms.
- Process Asynchronously: Handle webhook events asynchronously to avoid timeouts and ensure that your application can process a high volume of events without blocking the main request thread.
- Idempotent Operations: Design your webhook handlers to be idempotent. This means that processing the same event multiple times (which can happen due to network issues or retries) should have the same effect as processing it once.
- Use HTTPS: Ensure your webhook endpoint uses HTTPS to encrypt communication between Stripe and your server, protecting sensitive data in transit.
General Security Measures
- PCI DSS Compliance: If you handle raw card data directly (though Stripe's recommended approach is to use Stripe Elements or Checkout to avoid this), ensure your systems are PCI DSS compliant. Stripe itself maintains PCI DSS compliance, significantly reducing your burden by handling sensitive card data securely.
- Multi-Factor Authentication (MFA): Enable MFA for all user accounts accessing the Stripe Dashboard. This adds an extra layer of security beyond passwords.
- Audit Logs: Regularly review audit logs in your Stripe Dashboard to monitor API activity and detect any suspicious access patterns.
Adhering to these practices fortifies your integration against common security threats, safeguarding both your business and your customers' data.