Skip to main content

Web

Toffee Web SDK provides easy integration of offers into web games.

Installation​

npm install @toffeecom/sdk-web

Setup​

Initialize the SDK​

import { ToffeeSDK } from '@toffeecom/sdk-web';

// Use your publishable API key (safe for client-side code)
const toffee = new ToffeeSDK({ publishableKey: 'your_publishable_key' });

Fetch an Offer​

Basic Usage​

// Basic usage
const offer = await toffee.fetchOffer('user_123');

if (offer) {
console.log('Offer available:', offer);
console.log('Offer URL:', offer.url);
console.log('Offer image:', offer.image);
} else {
console.log('No offer available for this user');
}

User Metadata​

Provide relevant user metadata to get better targeted offers:

const userMetadata = {
level: '15',
achievements: 'speedster,collector',
playtime_hours: '120',
last_purchase: '2025-01-01',
};

const deviceData = {
id: '550e8400-e29b-41d4-a716-446655440000'
};

const offer = await toffee.fetchOffer('user_123', userMetadata, deviceData);

Configuration​

You can customize the SDK behavior with additional options:

const toffee = new ToffeeSDK({
publishableKey: 'your_publishable_key',
endpoint: 'https://services.sandbox.toffee.com', // Custom API endpoint (optional)
});

API Reference​

ToffeeSDK​

constructor({ publishableKey: string })

  • Initialize the SDK with your publishable key

constructor({ publishableKey: string, endpoint: string })

  • Initialize the SDK with your publishable key and custom endpoint
  • Use this variant when connecting to a custom endpoint

fetchOffer(userId: string, userMetadata?: UserMetadata, deviceData?: UserDeviceData): Promise<OfferDescriptor | null>

  • Returns offer descriptor object with URL and image, null if no offer

Types​

The SDK exports the following types:

import type {
OfferDescriptor
} from '@toffeecom/sdk-web';
interface OfferDescriptor {
id: string;
url: string;
status: string;
image?: string;
expires?: number; // Duration in seconds
}

Testing​

Use sandbox url during development:

const toffee = new ToffeeSDK({
publishableKey: 'your_sandbox_publishable_key',
endpoint: 'https://services.sandbox.toffee.com'
});