> ## Documentation Index
> Fetch the complete documentation index at: https://spreecommerce.org/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Authentication modes and guest checkout with the Spree SDK

The SDK supports multiple authentication modes depending on your use case. For a full overview of the API authentication methods, see the [Store API Authentication](/api-reference/store-api/authentication) reference.

## Publishable Key Only (Guest/Public Access)

Use a publishable API key for public endpoints like browsing products:

```typescript theme={"theme":"night-owl"}
import { createClient } from '@spree/sdk'

const client = createClient({
  baseUrl: 'http://localhost:3000',
  publishableKey: 'pk_xxx',
})
```

```typescript theme={"theme":"night-owl"}
// Public endpoints work without user authentication
const products = await client.products.list()
```

## Publishable Key + JWT (Authenticated Customer)

For authenticated customer actions like viewing orders or managing addresses:

```typescript theme={"theme":"night-owl"}
// Login to get a JWT token
const { token, user } = await client.auth.login({
  email: 'customer@example.com',
  password: 'password123',
})

// Use the token for authenticated requests
const orders = await client.customer.orders.list({}, { token })
```

```typescript theme={"theme":"night-owl"}
// Refresh token when needed
const newTokens = await client.auth.refresh({ token })
```

## Register New Customer

```typescript theme={"theme":"night-owl"}
const { token, user } = await client.customers.create({
  email: 'new@example.com',
  password: 'password123',
  password_confirmation: 'password123',
  first_name: 'John',
  last_name: 'Doe',
})
```

## Guest Checkout

For guest checkout, use the `token` returned when creating a cart. The SDK automatically sends it via the `x-spree-token` header:

```typescript theme={"theme":"night-owl"}
// Create a cart (guest)
const cart = await client.carts.create()

// Use spreeToken for all cart and checkout operations
const options = { spreeToken: cart.token }

// Add items
await client.carts.items.create(cart.id, {
  variant_id: 'variant_abc123',
  quantity: 1,
}, options)
```

```typescript theme={"theme":"night-owl"}
const options = { spreeToken: cart.token }

// Update cart with email
await client.carts.update(cart.id, {
  email: 'guest@example.com',
}, options)

// Complete checkout
await client.carts.complete(cart.id, options)
```
