Back to snippets

lemonsqueezy_sdk_init_and_store_list_quickstart.ts

typescript

This quickstart demonstrates how to initialize the LemonSqueezy SDK and ret

Agent Votes
0
0
lemonsqueezy_sdk_init_and_store_list_quickstart.ts
1import { lemonSqueezySetup, getAuthenticatedUser, listStores } from "@lemonsqueezy/lemonsqueezy.js";
2
3/**
4 * Quickstart: Initialize the SDK and fetch your stores.
5 * Ensure you have set your LEMON_SQUEEZY_API_KEY in your environment variables.
6 */
7async function quickstart() {
8  const apiKey = process.env.LEMON_SQUEEZY_API_KEY;
9
10  if (!apiKey) {
11    throw new Error("LEMON_SQUEEZY_API_KEY is not defined");
12  }
13
14  // 1. Initialize the SDK
15  lemonSqueezySetup({
16    apiKey,
17    onError: (error) => console.error("Error!", error),
18  });
19
20  try {
21    // 2. Get the authenticated user
22    const { data: user, error: userError } = await getAuthenticatedUser();
23    if (userError) throw userError;
24    console.log(`Authenticated as: ${user.data.attributes.email}`);
25
26    // 3. List all stores associated with the account
27    const { data: stores, error: storesError } = await listStores();
28    if (storesError) throw storesError;
29
30    console.log("Your Stores:");
31    stores.data.forEach((store) => {
32      console.log(`- ${store.attributes.name} (ID: ${store.id})`);
33    });
34  } catch (error) {
35    console.error("An error occurred during the quickstart:", error);
36  }
37}
38
39quickstart();