Identify the App Instance in a Site Widget Built with the CLI

For security reasons, the app instance ID isn’t directly accessible in site widgets or settings panels. Instead, you can securely extract it by sending a Wix access token to a backend API. The backend can decode the token, retrieve the instance ID, and perform any necessary business logic.

This article explains how to:

  • Backend: Expose a backend API that securely extracts the app instance ID from a Wix access token.
  • Frontend: Pass the Wix access token from your custom element to your backend.

Step 1 | Expose a backend API to extract the app instance ID

To securely identify the app instance:

  1. Add a Backend API Extension in the Wix CLI.

  2. In the api.ts file of your backend API extension, import the auth submodule from the Essentials SDK:

    Copy
  3. In the GET endpoint, call getTokenInfo(), which extracts instanceId from the access token:

    Copy
  4. Use the instanceId and return the relevant data.

Note: Access tokens sent from site widgets have a site visitor or member identity. If you want to call getAppInstance(), you'd need to elevate permissions.

Step 2 | Pass a Wix access token from your custom element to your backend

To send the app instance ID to your backend, include a Wix access token in the request.

  1. Add a Site Widget Extension in the CLI. If you already have a site widget, skip to the next step.

  2. In the element.tsx file of your site widget extension, import the httpClient submodule from the Essentials SDK:

    Copy
  3. Call fetchWithAuth() to send a request to your backend with a Wix access token:

    Copy

Tip: To get the app instance ID in the settings panel, call fetchWithAuth() from your panel.tsx file.

Example

The following sections contain example backend and frontend code for retrieving the app instance ID from a custom element.

Backend code

The following example is based on a CLI backend API extension called get-instance.

Copy

Frontend code

The following example creates a custom element to display the mock instance data from the get-instance endpoint.

Copy

See also

Did this help?