Skip to content

updatedotdev/js

Repository files navigation

Update JS Library

Update is a library for seamless billing and entitlement management. It integrates with your existing tools, like Stripe and Supabase, so you can integrate without migrating away from your existing stack.

πŸš€ Quickstart

The easiest way to get started with Update is to use the create-update-app command:

npm create update@latest

This tool will help you choose a framework and set up a fully working Update application in seconds. Just provide a name and your API keys.

For source code examples, check out our examples repository.

✨ Features

  • Billing: Seamless payments management
  • Entitlements: Simple access control for premium features
  • Framework Support: Built-in integration for Next.js and other SSR environments

πŸ”§ Installation

npm install @updatedev/js

πŸ” Auth Integrations

🏁 Getting Started

First, you need to create an account on Update and obtain your publishable key. This key is essential for initializing the Update client in your application. Additionally, configure your preferred authentication provider to manage user sessions and access control.

βš™οΈ Initializing

Basic Setup

import { createClient } from '@updatedev/js';

export async function createUpdateClient() {
  return createClient(process.env.NEXT_PUBLIC_UPDATE_PUBLISHABLE_KEY!, {
    getSessionToken: async () => {
      // This must be replaced with your own logic to get your session token
      // For example, with Supabase:
      //
      // import { createSupabaseClient } from '@/utils/supabase/client'
      // ...
      // const supabase = createSupabaseClient()
      // const { data } = await supabase.auth.getSession()
      // if (data.session == null) return
      // return data.session.access_token

      // For this example, we'll just return a static token
      return 'your-session-token';
    },
    environment: process.env.NODE_ENV === 'production' ? 'live' : 'test',
  });
}

Initialization options

  • getSessionToken: A function that returns a session token for the user. This is optional, but required for most functions that require authentication.
  • environment: The environment to use for the client. Valid values are live and test.

Environment Variables (.env.local)

NEXT_PUBLIC_UPDATE_PUBLISHABLE_KEY=

πŸ—οΈ Framework Integration

Next.js Integration

Update works well with Next.js and other SSR environments. Create a utils/update directory with these files:

Client (utils/update/client.ts)

import { createClient } from '@updatedev/js';

export async function createUpdateClient() {
  return createClient(process.env.NEXT_PUBLIC_UPDATE_PUBLISHABLE_KEY!, {
    getSessionToken: async () => {
      // This must be replaced with your own logic to get your session token
      // For example, with Supabase:
      //
      // import { createSupabaseClient } from '@/utils/supabase/client'
      // ...
      // const supabase = createSupabaseClient()
      // const { data } = await supabase.auth.getSession()
      // if (data.session == null) return
      // return data.session.access_token

      // For this example, we'll just return a static token
      return 'your-session-token';
    },
    environment: process.env.NODE_ENV === 'production' ? 'live' : 'test',
  });
}

Server (utils/update/server.ts)

import { createClient } from '@updatedev/js';

export async function createUpdateClient() {
  return createClient(process.env.NEXT_PUBLIC_UPDATE_PUBLISHABLE_KEY!, {
    getSessionToken: async () => {
      // This must be replaced with your own logic to get your session token
      // For example, with Supabase:
      //
      // import { createSupabaseClient } from '@/utils/supabase/server'
      // const supabase = await createSupabaseClient()
      // const { data } = await supabase.auth.getSession()
      // if (data.session == null) return
      // return data.session.access_token

      // For this example, we'll just return a static token
      return 'your-session-token';
    },
    environment: process.env.NODE_ENV === 'production' ? 'live' : 'test',
  });
}

πŸ’³ Billing Features

Getting Products

const { data, error } = await client.billing.getProducts();

Creating a Checkout Session

const { data, error } = await client.billing.createCheckoutSession(priceId, {
  redirect_url: 'http://localhost:3000/subscription',
});

Managing Subscriptions

Get user subscriptions:

const { data } = await client.billing.getSubscriptions();

Cancel a subscription:

await client.billing.updateSubscription(id, {
  cancel_at_period_end: true,
});

Reactivate a subscription:

await client.billing.updateSubscription(id, {
  cancel_at_period_end: false,
});

πŸ›‘οΈ Entitlements

List Entitlements

const { data, error } = await client.entitlements.list();

Check Entitlement

const { data, error } = await client.entitlements.check('premium');

πŸ“š Documentation

For complete documentation, visit our documentation.

πŸ’¬ Support

Need help? Join our Discord community for support and discussions.

🀝 License

MIT