If you prefer a typed experience over calling http endpoints directly, you can use our sdk @unprice/api.

Install

 npm install @unprice/api

Unprice Api key

When creating, revoking or updating resources, you will need your api key — you can create a new one in the settings of your project. Afterwards you need to provide it to the client:

import { Unprice } from "@unprice/api";

const unprice = new Unprice({ token: "<API_KEY>" });

Always keep your api key safe and reset it if you suspect it has been compromised.

Response format

Because forgetting to handle thrown errors properly in javascript is often forgotten, we have decided to explicitly return errors to be handled. Fortunately typescript helps us here and everything is typesafe.

Every method returns either an error or a result field, never both and never none.

{
  result: T // the result depends on what method you called
}

Checking for errors

To check for errors you use the error property, our errors are easy to read and provide a link to our documentation for more information.

import { Unprice } from "@unprice/api";

const unprice = new Unprice({
  token: env.UNPRICE_API_KEY,
  baseUrl: env.UNPRICE_API_URL
})

const { result, error } = await unprice.customers.getPaymentMethods({
  customerId: "cus_123",
  provider: "stripe"
});

if (error) {
  // handle potential network or bad request error
  // a link to our docs will be in the `error.docs` field
  console.error(error.message);
  return;
}

// return the payment methods
return result

Options

The constructor accepts some options to customize the behavior:

Base Url

Run all requests against your own instance of unprice hosted on your own infrastructure.

baseUrl
string
default:"https://api.unprice.dev"
const unprice = new Unprice({
  //...
  baseUrl: "https://my.domain"
})

Retries

By default the client will retry on network errors, you can customize this behavior:

retry
const unprice = new Unprice({
  // ...
  retry: {
    attempts: 3,
    backoff: (retryCount) => retryCount * 1000
  }
})

Cache

Configure the fetch cache behavior.

As of October 2023, the cache option is not yet implemented in cloudflare workers and will throw an error if used.

cache
string

Available options are: default, force-cache, no-cache, no-store, only-if-cached or reload.

const unprice = new Unprice({
  // ...
  cache: "no-cache"
})

Disable telemetry

By default, Unprice collects anonymous telemetry data to help us understand which versions of our SDK is being used, and in which environment.

If you wish to disable this, you can do so by passing a boolean flag to the constructor:

const unprice = new Unprice({
  disableTelemetry: true
})