Automating Stripe Product Sync: Building the sync_stripe_catalog Command

Published on 23 Mar 2026 Tech Development
image
Photo by Christin Hume on Unsplash
Disclaimer: This post was created with the assistance of Gemini and/or ChatGPT for informational purposes. While given a quick look over, readers are encouraged to verify key facts independently.

This follow-up guide picks up where the environment setup left off. Now that your Stripe CLI and API keys are ready, we will build the actual logic to mirror your Stripe Product dashboard within your Django database.

Phase 1: Defining the Data Schema

1. Create Stripe-Compatible Models

In your models.py, define structures that match Stripe's object hierarchy. This allows you to store Product and Price data locally for faster queries.

from django.db import models

class Product(models.Model):
stripe_id = models.CharField(max_length=255, unique=True)
name = models.CharField(max_length=255)
description = models.TextField(blank=True, null=True)
active = models.BooleanField(default=True)

class Price(models.Model):
stripe_id = models.CharField(max_length=255, unique=True)
product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="prices")
unit_amount = models.IntegerField()  # Stored in cents
currency = models.CharField(max_length=3, default='usd')

Phase 2: The Sync Logic

2. Create the Management Directory

Django looks for custom commands in a specific folder structure. Create these directories (and empty init.py files) inside your app:

your_app/management/commands/sync_stripe_catalog.py

3. Write the Sync Script

Inside sync_stripe_catalog.py, use the update_or_create method to ensure your local database matches the Stripe API without creating duplicates:

import stripe
from django.conf import settings
from django.core.management.base import BaseCommand
from your_app.models import Product, Price

stripe.api_key = settings.STRIPE_SECRET_KEY

class Command(BaseCommand):
def handle(self, *args, **options):
products = stripe.Product.list(active=True)
for sp in products:
local_product, _ = Product.objects.update_or_create(
stripe_id=sp.id,
defaults={'name': sp.name, 'description': sp.description}
)

        prices = stripe.Price.list(product=sp.id, active=True)
        for p in prices:
            Price.objects.update_or_create(
                stripe_id=p.id,
                defaults={
                    'product': local_product,
                    'unit_amount': p.unit_amount,
                    'currency': p.currency
                }
            )
    self.stdout.write("Catalog synced successfully.")

Phase 3: Execution

4. Run the Migration

Apply your model changes to the database:

python manage.py makemigrations
python manage.py migrate

5. Perform the Initial Sync

Trigger your new command to pull data from your Stripe Dashboard into your local environment:

python manage.py sync_stripe_catalog

6. Verify Local Data

Open the Django shell or Admin panel to confirm that your Product and Price tables are now populated with your Stripe data.

Comments