### Create Update App Source: https://github.com/updatedotdev/js/blob/main/README.md Uses npx to run the create-update-app command-line tool, which scaffolds a new Update application. It guides users through framework selection and initial setup. ```bash npm create update@latest ``` -------------------------------- ### Install Update JS Library Source: https://github.com/updatedotdev/js/blob/main/README.md Installs the Update JavaScript library using npm. This is the primary step to integrate Update's billing and entitlement features into your project. ```bash npm install @updatedev/js ``` -------------------------------- ### Next.js Client Integration Source: https://github.com/updatedotdev/js/blob/main/README.md Provides a TypeScript example for integrating the Update client within a Next.js application, specifically for client-side logic. It shows how to create a reusable client instance. ```typescript 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', }); } ``` -------------------------------- ### Get Products Source: https://github.com/updatedotdev/js/blob/main/README.md Fetches available products from the Update service. This is a common operation for displaying pricing information or managing subscriptions. ```typescript const { data, error } = await client.billing.getProducts(); ``` -------------------------------- ### Get User Subscriptions (TypeScript) Source: https://github.com/updatedotdev/js/blob/main/README.md Fetches all active and inactive subscriptions for the current user. Requires an authenticated client instance. Returns subscription data. ```typescript const { data } = await client.billing.getSubscriptions(); ``` -------------------------------- ### Initialize Update Client Source: https://github.com/updatedotdev/js/blob/main/README.md Demonstrates how to initialize the Update client in a TypeScript application. It requires a publishable key and optionally accepts a function to retrieve session tokens and an environment setting. ```typescript 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', }); } ``` -------------------------------- ### Create Checkout Session Source: https://github.com/updatedotdev/js/blob/main/README.md Initiates a checkout session for a specific price ID, allowing users to complete a purchase or subscription. It requires a redirect URL for post-checkout navigation. ```typescript const { data, error } = await client.billing.createCheckoutSession(priceId, { redirect_url: 'http://localhost:3000/subscription', }); ``` -------------------------------- ### Next.js Server Integration Source: https://github.com/updatedotdev/js/blob/main/README.md Illustrates how to set up the Update client for server-side operations in a Next.js application. This is crucial for secure backend interactions and data fetching. ```typescript 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', }); } ``` -------------------------------- ### List Entitlements (TypeScript) Source: https://github.com/updatedotdev/js/blob/main/README.md Retrieves all entitlements associated with the current user. Requires an authenticated client instance. Returns a list of entitlement objects. ```typescript const { data, error } = await client.entitlements.list(); ``` -------------------------------- ### Check Entitlement (TypeScript) Source: https://github.com/updatedotdev/js/blob/main/README.md Verifies if the user possesses a specific entitlement. Requires the entitlement key and a client instance. Returns entitlement status. ```typescript const { data, error } = await client.entitlements.check('premium'); ``` -------------------------------- ### Reactivate Subscription (TypeScript) Source: https://github.com/updatedotdev/js/blob/main/README.md Reverses a pending cancellation for a subscription. Requires the subscription ID and a client instance. Sets `cancel_at_period_end` to false. ```typescript await client.billing.updateSubscription(id, { cancel_at_period_end: false, }); ``` -------------------------------- ### Cancel Subscription (TypeScript) Source: https://github.com/updatedotdev/js/blob/main/README.md Marks a subscription for cancellation at the end of its current billing period. Requires the subscription ID and a client instance. Updates the subscription status. ```typescript await client.billing.updateSubscription(id, { cancel_at_period_end: true, }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.