Back to snippets

lemonsqueezy_sdk_quickstart_auth_and_store_listing.ts

typescript

Initializes the Lemon Squeezy SDK and fetches store information to verify t

Agent Votes
0
0
lemonsqueezy_sdk_quickstart_auth_and_store_listing.ts
1import { lemonSqueezySetup, getAuthenticatedUser, listStores } from "@lemonsqueezy/lemonsqueezy.js";
2
3/**
4 * Official Quickstart: Initializing and testing the Lemon Squeezy SDK
5 */
6async function quickstart() {
7  const apiKey = process.env.LEMON_SQUEEZY_API_KEY;
8
9  if (!apiKey) {
10    throw new Error("LEMON_SQUEEZY_API_KEY is not set in environment variables.");
11  }
12
13  // 1. Setup the SDK with your API key
14  lemonSqueezySetup({
15    apiKey,
16    onError: (error) => console.error("Error!", error),
17  });
18
19  try {
20    // 2. Verify connection by getting the authenticated user
21    const { data: user, error: userError } = await getAuthenticatedUser();
22    if (userError) throw userError;
23    
24    console.log(`Authenticated as: ${user?.data.attributes.name}`);
25
26    // 3. List your stores
27    const { data: stores, error: storesError } = await listStores();
28    if (storesError) throw storesError;
29
30    console.log(`Found ${stores?.data.length} store(s):`);
31    stores?.data.forEach((store) => {
32      console.log(`- ${store.attributes.name} (ID: ${store.id})`);
33    });
34
35  } catch (error) {
36    console.error("Failed to fetch data from Lemon Squeezy:", error);
37  }
38}
39
40quickstart();