Creating Stripe price with custom ID via API

545 views Asked by At

How do I create a Stripe price with a custom ID thru their API?

I can create prices with custom IDs in the Stripe Dashboard, but when I create using the API I get this error:

Received unknown parameter: id

For example, I am using this code to synchronize the test environment with the same prices as the live environment, but I can't get the id to sync:

$testClient->prices->create([
        'id'          => $livePrice->id,
        'product'     => $livePrice->product,
        'nickname'    => $livePrice->nickname,
        'currency'    => $livePrice->currency,
        'unit_amount' => $livePrice->unit_amount,
        'active'      => $livePrice->active,
    ]);

For some reason I don't see the Stripe API price create endpoint has support for creating prices with a custom ID but I am wondering if maybe I am just doing something wrong here. Seems very strange they'd leave that off the API for prices but include it in the dashboard and on the products API.

2

There are 2 answers

2
Lucky2501 On BEST ANSWER

As answered above, it's not possible.

Some Stripe accounts had the ability to set custom IDs on Prices but that feature was removed.

What I would add to @quine9997's answer is that you should consider setting the Price's nickname as it's lookup_key.
The reason being you can actually list Prices by lookup_key, while you can't by nickname.

So let's say you have this call to create your Price:

$stripe->prices->create([
  'unit_amount' => 1999,
  'currency' => 'usd',
  'recurring' => ['interval' => 'month'],
  'product' => $product_id,
  'lookup_key' => $your_custom_value
]);

If you do this on different Stripe accounts (or on live / test mode for the same account), this will give you two different Price IDs.

But you can just get it regardless of the account with this code:

$stripe->prices->all(['lookup_keys' => [$your_custom_value]]);

This will give you a list, containing (hopefully) one Price.

2
quine9997 On

The Stripe API does not allow you to create prices with custom IDs. This is because prices are automatically assigned a unique ID by Stripe when they are created.

If you need to sync prices between different environments, you can do so by using the Stripe API to retrieve the prices from the live environment and then creating new prices in the test environment with the same data.

Here is an example of how to do this using the Python Stripe client.

This will create new prices in the test environment with the same data as the prices in the live environment. However, the new prices will have different IDs.

import stripe

# Retrieve the prices from the live environment
live_client = stripe.Client("YOUR_LIVE_API_KEY")
live_prices = live_client.prices.list()

# Create new prices in the test environment
test_client = stripe.Client("YOUR_TEST_API_KEY")
for live_price in live_prices:
    test_client.prices.create(
        product=live_price.product,
        nickname=live_price.nickname,
        currency=live_price.currency,
        unit_amount=live_price.unit_amount,
        active=live_price.active,
    )

Update. PHP code

<?php

require 'vendor/autoload.php';

use Stripe\Stripe;

// Retrieve the prices from the live environment
$live_client = new Stripe\StripeClient('YOUR_LIVE_API_KEY');
$live_prices = $live_client->prices->all();

// Create new prices in the test environment
$test_client = new Stripe\StripeClient('YOUR_TEST_API_KEY');
foreach ($live_prices as $live_price) {
    $test_client->prices->create([
        'product' => $live_price['product'],
        'nickname' => $live_price['nickname'],
        'currency' => $live_price['currency'],
        'unit_amount' => $live_price['unit_amount'],
        'active' => $live_price['active'],
    ]);
}


Observations.

  1. The PHP code uses the Stripe PHP SDK ;

  2. Stripe PHP SDK is a wrapper around the Stripe REST API

  3. You can install Stripe PHP SDK using Composer.

  4. These are the main steps

4.A Include the vendor/autoload.php ;

4.B Create a new Stripe client object by passing your Stripe API key to the constructor. You can then use the client object to interact with the Stripe API.

4.C. In the code above, we first create a new live client object using the live API key. We then use the live client object to retrieve all of the prices from the live environment.

4.D. Next, we create a new test client object using the test API key. We then iterate over the live prices and create a new price in the test environment for each live price. 4.E. Finally, we save the changes to the test environment.