API (nube.api)
The nube.api namespace groups typed adapters that let your app invoke host-side capabilities from inside the Web Worker. Each adapter mirrors a host domain (analytics, checkout, …) and crosses the worker / main-thread boundary through the SDK's internal command channel.
import type { NubeSDK } from "@tiendanube/nube-sdk-types";
export function App(nube: NubeSDK) {
const analytics = nube.api.getAnalytics();
const checkout = nube.api.getCheckout();
}
Adapters
| Adapter | Accessor | Description |
|---|---|---|
| Analytics | nube.api.getAnalytics() | Send analytics events to the store's configured providers (e.g. GA4). |
| Checkout | nube.api.getCheckout() | Invoke checkout host operations (hide payment options, benefits, …). |
- Analytics → — full reference for
getAnalytics(). - Checkout → — full reference for
getCheckout().
How it works
Every accessor returns a cached adapter instance — calling it more than once gives you the same object, so identity comparisons hold:
nube.api.getAnalytics() === nube.api.getAnalytics(); // true
Materialization is lazy: apps that never touch nube.api pay no setup cost. The underlying command transport is created on the first get*() call and stays subscribed for the lifetime of the worker.
Return values and errors
All adapter methods are asynchronous and return a Promise, since the actual work happens on the main thread. When a command fails, the promise rejects with a standard Error whose message has the form "<code>: <message>", where <code> is one of:
| Code | Meaning |
|---|---|
unknown_command | The host doesn't recognize the requested command. |
command_failed | The host received the command but failed while executing it. |
timeout | The host did not respond within the allotted time. |
import type { NubeSDK } from "@tiendanube/nube-sdk-types";
export async function App(nube: NubeSDK) {
const analytics = nube.api.getAnalytics();
try {
await analytics.event("add_to_cart", { value: 49.9 });
} catch (error) {
console.error(error.message); // e.g. "timeout: ..."
}
}
Help us improve NubeSDK
Found an issue or have a suggestion? Let us know on GitHub.