UNPKG

@tiun/sdk

Version:

tiun SDK for payments and subscriptions

142 lines (99 loc) • 8.25 kB
# tiun SDK The tiun SDK provides a simple API for integrating payments, subscriptions, and authentication into any website or app. **Full documentation: [docs.tiun.io](https://docs.tiun.io)** --- ## Installation ```bash npm install @tiun/sdk ``` No build step? Load it from a CDN — see [Installation](https://docs.tiun.io/sdk/web-sdk/installation). --- ## Quick start ```typescript import { tiun } from '@tiun/sdk'; tiun.init({ snippetId: 'your-snippet-id', language: 'en', // 'en' | 'de' | 'fr', match your site sandbox: true // local development and testing; false in production }); tiun.checkout({ productId: 'prod_pro' }); tiun.on('userChange', ({ isAuthenticated, user }) => { showPro(isAuthenticated && user.productAccess.includes('prod_pro')); }); ``` Call `tiun.init()` once, at your app's root. The snippet loads automatically — no script tags needed. --- ## API at a glance ### Init options Details: [Initialization](https://docs.tiun.io/sdk/web-sdk/initialization) | Option | Type | Default | Description | | ------------ | ----------------------------- | ------------ | ---------------------------------------------------------- | | `snippetId` | `string` | — | **Required.** Your snippet ID from the dashboard. | | `language` | `'en' \| 'de' \| 'fr'` | `'en'` | Language of the snippet UI. | | `tone` | `'formal' \| 'informal'` | `'informal'` | Tone of the snippet UI. | | `appearance` | `'light' \| 'dark' \| 'auto'` | `'light'` | Color theme. `'auto'` follows the visitor's OS. | | `sandbox` | `boolean` | `false` | Use the sandbox environment instead of production. | | `debug` | `boolean` | `false` | Log to the console. | | `on<Event>` | `function` | — | Event callbacks, e.g. `onUserChange`. Same as `tiun.on()`. | ### Methods Details: [Core methods](https://docs.tiun.io/sdk/web-sdk/core-methods) | Method | Returns | Description | | ------------------------------------------------- | ----------------------------- | ------------------------------------------------------- | | `init(config)` | `TiunSDK` | Initialize the SDK. | | `checkout({ productId, referenceId? })` | `Promise<void>` | Open checkout for a product. | | `start()` | `Promise<void>` | Open the time-based connect flow. | | `login()` | `Promise<void>` | Open the login modal. | | `logout()` | `void` | Log the user out. | | `getUser()` | `GetUserResponse` | Cached user state. | | `getUserVerificationToken()` | `Promise<string \| null>` | Token for verifying the user on your server. | | `listProducts()` | `Promise<Product[]>` | Products on this snippet. | | `getProduct(productId)` | `Promise<ProductDetail>` | A product's details and localized price. | | `listProductAccess()` | `Promise<ProductAccess[]>` | The user's active access. | | `checkProductAccess(productId, { referenceId? })` | `Promise<ProductAccessCheck>` | Whether the user has access to a product. | | `setContent(options)` | `Promise<void>` | Tell tiun what content the user is viewing. | | `setAppearance(appearance)` | `void` | Switch the color theme at runtime. | | `on(event, callback)` / `once(event, callback)` | `() => void` | Subscribe to an event. Returns an unsubscribe function. | | `waitForReady()` | `Promise<void>` | Resolves when the snippet is ready. | | `destroy()` | `void` | Remove the SDK's listeners and reset its state. | ### Events Details: [Events](https://docs.tiun.io/sdk/web-sdk/events) | Event | Payload | Fired when | | ------------- | ------------------ | ------------------------------------------------------------------- | | `ready` | — | The snippet is ready. | | `userChange` | `UserChangeEvent` | Init, login, logout, or any change to the user's access or profile. | | `login` | `LoginEvent` | The user logged in. | | `logout` | — | The user logged out. | | `paywallShow` | `PaywallShowEvent` | The paywall should be shown. | | `paywallHide` | `PaywallHideEvent` | The paywall should be hidden. | | `error` | `TiunError` | An error occurred. | ### Properties Details: [Properties](https://docs.tiun.io/sdk/web-sdk/properties) | Property | Type | Description | | ----------------- | ------------------ | --------------------------------- | | `version` | `string` | SDK version. | | `isInitialized` | `boolean` | Whether `init()` has been called. | | `isReady` | `boolean` | Whether the snippet is ready. | | `isAuthenticated` | `boolean` | Whether the user has a session. | | `user` | `UserInfo \| null` | The current user, or `null`. | ### Types Bundled with the package: `TiunConfig`, `TiunSDK`, `TiunError`, `UserInfo`, `GetUserResponse`, `UserChangeEvent`, `LoginEvent`, `PaywallShowEvent`, `PaywallHideEvent`, `CheckoutOptions`, `ContentOptions`, `ContentType`, `MediaType`, `SupportedLanguage`, `Appearance`, `Product`, `ProductDetail`, `ProductType`, `ProductAccess`, `ProductAccessCheck`, `ProductAccessOptions`, `TiunEventType`. ```typescript import { tiun, type UserInfo } from '@tiun/sdk'; ``` --- ## Guides - [Authenticate your users](https://docs.tiun.io/guides/authentication/authenticate-your-user) and [verify them server-side](https://docs.tiun.io/guides/authentication/verify-server-side) - [Sell subscriptions](https://docs.tiun.io/guides/subscriptions/monetize-with-subscriptions) - [Sell one-time products](https://docs.tiun.io/guides/one-time-purchases/sell-one-time-products) - [Repeatable purchases](https://docs.tiun.io/guides/one-time-purchases/repeatable-one-time-purchases) — tickets, single articles, anything bought more than once - [Charge for time-based sessions](https://docs.tiun.io/guides/time-based-billing/charge-for-time-based-sessions) - [Test your integration](https://docs.tiun.io/guides/testing/test-flows) in the [sandbox](https://docs.tiun.io/reference/generic/sandbox) --- ## Framework examples [Vue](https://docs.tiun.io/sdk/web-examples/vue) · [React](https://docs.tiun.io/sdk/web-examples/react) · [Nuxt](https://docs.tiun.io/sdk/web-examples/nuxt) · [Vanilla JS](https://docs.tiun.io/sdk/web-examples/vanilla-js) Building a React Native app? Use [`@tiun/react-native-sdk`](https://docs.tiun.io/sdk/react-native-sdk/installation). --- ## Changelog See [What's new](https://docs.tiun.io/changelog). ## License MIT