Polar SDK

0.31.3 · active · verified Fri Apr 17

The `polar-sdk` is the official Python client for the Polar API, designed to help developers integrate subscription and monetization features into their applications. It provides access to resources like products, subscriptions, and webhooks. As of version 0.31.3, it offers both synchronous and asynchronous interfaces and is actively maintained with frequent updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `Polar` client, fetch a list of subscriptions, and handle potential API errors. It uses an asynchronous client, which is recommended for modern Python applications. Ensure you have `POLAR_API_KEY` set in your environment or replace the placeholder with your actual API key.

import os
from polar import Polar
from polar.exceptions import PolarError

# Initialize the Polar client. It automatically uses the POLAR_API_KEY
# environment variable if set. Otherwise, you can pass it directly.
# For testing, you might use a dummy key if not interacting with a live API.
client = Polar(auth=os.environ.get("POLAR_API_KEY", "sk_test_..._your_key"))

async def main():
    try:
        # Example: List subscriptions (using the async client)
        print("Attempting to list subscriptions...")
        subscriptions_page = await client.subscriptions.list(limit=5)

        if subscriptions_page.items:
            print(f"Found {len(subscriptions_page.items)} subscriptions:")
            for sub in subscriptions_page.items:
                print(f"  - ID: {sub.id}, Status: {sub.status}, Product ID: {sub.product_id}")
        else:
            print("No subscriptions found.")

    except PolarError as e:
        print(f"Polar API Error: {e.status_code} - {e.body}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    # For running async code in a sync context (e.g., top-level script)
    import asyncio
    asyncio.run(main())

view raw JSON →