# Vanilla JS

The tiun SDK can be loaded directly in a browser using a module script tag — no build tools needed. Import from unpkg (or any CDN that serves the package).

***

## Initialize

Load the SDK as an ES module and call `init()`.

```html
<script type="module">
  import { tiun } from 'https://unpkg.com/@tiun/sdk/tiun.js';

  tiun.init({
    snippetId: 'YOUR_SNIPPET_ID',
    language: 'en', // 'en' | 'de' | 'fr'
  });
</script>
```

***

## Checkout

Attach checkout to a button click.

```html
<button id="btn-checkout">Subscribe</button>

<script type="module">
  import { tiun } from 'https://unpkg.com/@tiun/sdk/tiun.js';

  tiun.init({ snippetId: 'YOUR_SNIPPET_ID', language: 'en' });

  document.getElementById('btn-checkout').onclick = () => {
    tiun.checkout({ productId: 'YOUR_PRODUCT_ID' });
  };
</script>
```

***

## Listen for user changes

Update the page when authentication or access changes.

```html
<div id="status">Not logged in</div>

<script type="module">
  import { tiun } from 'https://unpkg.com/@tiun/sdk/tiun.js';

  tiun.init({ snippetId: 'YOUR_SNIPPET_ID', language: 'en' });

  tiun.on('userChange', (data) => {
    const el = document.getElementById('status');

    if (data.isAuthenticated) {
      el.textContent = 'Logged in as ' + data.user.email;
    } else {
      el.textContent = 'Not logged in';
    }
  });
</script>
```

***

## Login and logout

```html
<button id="btn-login">Log in</button>
<button id="btn-logout">Log out</button>

<script type="module">
  import { tiun } from 'https://unpkg.com/@tiun/sdk/tiun.js';

  tiun.init({ snippetId: 'YOUR_SNIPPET_ID', language: 'en' });

  document.getElementById('btn-login').onclick = () => {
    tiun.login();
  };

  document.getElementById('btn-logout').onclick = () => {
    tiun.logout();
  };
</script>
```

***

## Gate content

Show or hide elements based on `productAccess`.

```html
<div id="premium" style="display:none">Premium content here</div>
<div id="upgrade">
  <p>Subscribe to access premium content.</p>
  <button id="btn-upgrade">Upgrade</button>
</div>

<script type="module">
  import { tiun } from 'https://unpkg.com/@tiun/sdk/tiun.js';

  tiun.init({ snippetId: 'YOUR_SNIPPET_ID', language: 'en' });

  document.getElementById('btn-upgrade').onclick = () => {
    tiun.checkout({ productId: 'YOUR_PRODUCT_ID' });
  };

  tiun.on('userChange', (data) => {
    const hasAccess = data.isAuthenticated &&
      data.user.productAccess.includes('YOUR_PRODUCT_ID');

    document.getElementById('premium').style.display = hasAccess ? 'block' : 'none';
    document.getElementById('upgrade').style.display = hasAccess ? 'none' : 'block';
  });
</script>
```

***

## Read properties

Check SDK state at any point using properties.

```html
<button id="btn-status">Show status</button>
<pre id="output"></pre>

<script type="module">
  import { tiun } from 'https://unpkg.com/@tiun/sdk/tiun.js';

  tiun.init({ snippetId: 'YOUR_SNIPPET_ID', language: 'en' });

  document.getElementById('btn-status').onclick = () => {
    const output = document.getElementById('output');
    output.textContent = JSON.stringify({
      version: tiun.version,
      isReady: tiun.isReady,
      isAuthenticated: tiun.isAuthenticated,
      user: tiun.user
    }, null, 2);
  };
</script>
```

***

## Paywall events (time-based)

For time-based billing, use paywall events to show or hide content.

```html
<div id="paywall">
  <h2>Premium content</h2>
  <button id="btn-access">Get access</button>
</div>
<div id="content" style="display:none">Your premium content</div>

<script type="module">
  import { tiun } from 'https://unpkg.com/@tiun/sdk/tiun.js';

  tiun.init({ snippetId: 'YOUR_SNIPPET_ID', language: 'en' });

  document.getElementById('btn-access').onclick = () => {
    tiun.start();
  };

  tiun.on('paywallHide', () => {
    document.getElementById('paywall').style.display = 'none';
    document.getElementById('content').style.display = 'block';
  });

  tiun.on('paywallShow', () => {
    document.getElementById('paywall').style.display = 'block';
    document.getElementById('content').style.display = 'none';
  });
</script>
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tiun.io/sdk/examples/vanilla-js.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
