npm package: @harshasomisetty/storefront
Description: Gameshift Storefront SDK
Latest version: 0.1.0-alpha.4

# Gameshift Storefront SDK

The Gameshift Storefront SDK enables seamless integration of Gameshift's checkout experience into your web application. It provides a simple way to sell digital assets, NFTs, and in-game items directly to your users.

## Installation

```bash
npm install @gameshift/storefront
# or
yarn add @gameshift/storefront
```

## Quick Start

```typescript
import { Storefront, Environment } from '@gameshift/storefront';

// Initialize the SDK
const storefront = new Storefront({
  apiKey: 'your-api-key',
  environment: Environment.Development, // or Environment.Production
});

// Purchase an item
try {
  const result = await storefront.purchaseSkuImmediately('sku_123', {
    userReferenceId: 'user_123',
    quantity: 1,
  });

  if (result.success) {
    console.log('Purchase successful:', result.data);
  } else {
    console.error('Purchase failed:', result.error);
  }
} catch (error) {
  console.error('Error:', error);
}
```

## Configuration

The SDK accepts the following configuration options:

```typescript
interface StorefrontConfig {
  apiKey: string; // Your Gameshift API key
  environment: Environment; // Development or Production
  baseUrl?: string; // Optional custom API endpoint
}
```

## API Reference

### purchaseSkuImmediately

Initiates an immediate purchase flow for a single SKU. This will open a checkout overlay where users can complete their purchase.

```typescript
purchaseSkuImmediately(
  skuId: string,
  options: {
    userReferenceId: string; // Unique identifier for the user
    quantity?: number;       // Optional: Number of items to purchase (default: 1)
    data?: Record<string, unknown>; // Optional: Additional data to pass with the purchase
  }
): Promise<Result<{
  skuId: string;
  quantity: number;
  transactionId: string;
}>>
```

#### Example

```typescript
const result = await storefront.purchaseSkuImmediately('sku_123', {
  userReferenceId: 'user_123',
  quantity: 1,
  data: {
    customField: 'value',
  },
});

if (result.success) {
  const { transactionId, skuId, quantity } = result.data;
  // Handle successful purchase
} else {
  // Handle error
  console.error(result.error);
}
```

## Checkout Flow

When `purchaseSkuImmediately` is called, the SDK will:

1. Open an overlay with the checkout interface
2. Allow users to choose their payment method (crypto or credit card)
3. Handle wallet connections if needed
4. Process the payment
5. Return the transaction result

## Types

### Environment

```typescript
enum Environment {
  Development = 'Development',
  Production = 'Production',
}
```

### Result

```typescript
interface Result<T> {
  success: boolean;
  data?: T;
  error?: string;
}
```

## Error Handling

The SDK uses a Result type that includes both success and error states. Always check the `success` boolean before accessing the `data` property:

```typescript
const result = await storefront.purchaseSkuImmediately('sku_123', {
  userReferenceId: 'user_123',
});

if (!result.success) {
  console.error('Purchase failed:', result.error);
  return;
}

// Safe to access result.data here
console.log('Transaction ID:', result.data.transactionId);
```

## Browser Support

The SDK supports all modern browsers:

- Chrome/Edge (latest 2 versions)
- Firefox (latest 2 versions)
- Safari (latest 2 versions)

## Development Status

This SDK is currently in alpha. APIs may change without notice.

## Support

For support, please contact support@gameshift.dev or visit our [documentation](https://docs.gameshift.dev).

## License

MIT