Back to Spree

Quickstart

docs/developer/sdk/quickstart.mdx

5.4.22.4 KB
Original Source

Installation

bash
npm install @spree/sdk
# or
yarn add @spree/sdk
# or
pnpm add @spree/sdk

Quick Start

typescript
import { createClient } from '@spree/sdk';

// Initialize the client
const client = createClient({
  baseUrl: 'https://api.mystore.com',
  publishableKey: 'pk_xxx',   // Store API
});

// Browse products (Store API)
const products = await client.products.list({
  limit: 10,
  expand: ['variants', 'media'],
});

// Get a single product
const product = await client.products.get('spree-tote');

// Authentication
const { token, user } = await client.auth.login({
  email: '[email protected]',
  password: 'password123',
});

// Create a cart and add items
const cart = await client.carts.create();
await client.carts.items.create(cart.id, {
  variant_id: 'var_abc123',
  quantity: 2,
}, { spreeToken: cart.token });

// Checkout flow
await client.carts.complete(cart.id, { spreeToken: cart.token });

Nested Resources

The SDK uses a resource builder pattern for nested resources:

Parent ResourceNested ResourceAvailable Methods
cartsitemscreate, update, delete
cartspaymentslist, get, create
cartspaymentMethodslist
cartspaymentSessionscreate, get, update, complete
cartsfulfillmentslist, update
cartsdiscountCodesapply, remove
cartsgiftCardsapply, remove
cartsstoreCreditsapply, remove
customeraddresseslist, get, create, update, delete, markAsDefault
customercreditCardslist, get, delete
customergiftCardslist, get
customerorderslist
customerpaymentSetupSessionscreate, get, complete
policieslist, get
categoriesproductslist
wishlistsitemscreate, update, delete
typescript
// Nested resources follow the pattern: client.parent.nested.method(parentId, ...)
await client.carts.items.create(cartId, params, options);
await client.carts.payments.list(cartId, options);
await client.carts.fulfillments.update(cartId, fulfillmentId, params, options);
await client.customer.addresses.list({}, options);
await client.categories.products.list(categoryId, params, options);
await client.wishlists.items.create(wishlistId, params, options);