In a custom element for a self-hosted site widget or site plugin, you need to use a Wix Client to call Wix APIs using the SDK.
You need to provide Wix with a function that injects your client with the access token:
this.accessTokenListener = wixClient.auth.getAccessTokenInjector();
With this setup, Wix injects your client with an access token and you can use your client to call SDK methods.
The following example uses an authenticated client to call:
products.queryProducts
from the @wix/stores
backend moduleseo.title
from the @wix/site-seo
frontend moduleimport { site } from "@wix/site";
import { createClient } from "@wix/sdk";
import { products } from "@wix/stores";
import { seo } from "@wix/site-seo";
const myWixClient = createClient({
auth: site.auth(),
host: site.host({ applicationId: "<your_app_id>" }),
modules: {
products,
seo,
},
});
class MyCustomElement extends HTMLElement {
constructor() {
super();
this.accessTokenListener = myWixClient.auth.getAccessTokenInjector();
}
connectedCallback() {
myWixClient.products
.queryProducts()
.find()
.then((productsQueryResult) => {
console.log("Products query result:", productsQueryResult);
});
myWixClient.seo.title().then((title) => {
console.log("Site title:", title);
});
}
}
customElements.define(tagName, MyCustomElement);