npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wix/site

v1.19.0

Published

The Site API enables code within Embedded Scripts and Site Widgets created with the CLI to interact with a Wix site. For example, you can access site data and interact with other Wix Apps, such as Wix Stores and Wix Bookings.

Downloads

372

Readme

Site API

The Site API enables code within Embedded Scripts and Site Widgets created with the CLI to interact with a Wix site. For example, you can access site data and interact with other Wix Apps, such as Wix Stores and Wix Bookings.

Note: This API is only available for use in Wix Apps that run within a Wix site environment. It isn't compatible with Velo code executed in the Wix Editor or on sites not hosted by Wix.

Setup

Install the @wix/site package using npm or Yarn:

npm install @wix/site

or

yarn add @wix/site

Next, create a Wix Client with the relevant frontend modules. Supported modules are listed under Frontend Modules in the menu of this SDK reference. The following example uses the seo module:

import { site } from "@wix/site";
import { createClient } from "@wix/sdk";
import { seo } from "@wix/site-seo"; // Frontend module of your choice

const client = createClient({
  host: site.host({ applicationId: "<your_app_id>" }),
  modules: {
    seo,
  },
});

Note: You can find your app ID in the OAuth page of the Wix Dev Center.

Finally, use the client constant to interact with the site via the frontend module. For example:

client.seo.title().then((title) => {
  console.log("Site title:", title);
});

Usage in Embedded Scripts

You can create a Wix Client in one of your scripts inside your embedded html when you create an Embedded Script extension.

Create a Wix Client in your script

In your script, you can now create a Wix Client and use it to interact with the site. Creating the client differs based on the type of the script:

type="module" (ESM)

Define a script to inject an access token to

When you create an Embedded Script extension, you provide an html snippet in which you can define script tags to load and execute Javascript. If you need to interact with Wix REST APIs or other Site APIs, you'll need to define a script tag that will have an access token injected into (only a single script tag can be used for this purpose). Add the accesstoken="true" attribute to the script tag to indicate that the script should be injected with an access token.

<script accesstoken="true" src="<your script source>"></script>
Inject an access token into the script

In a script of type module (ESM), the script should export a function called injectAccessTokenFunction that will be called by Wix to inject the access token into the script. The function should be created by calling getAccessTokenInjector on the auth object of the Wix Client.

import { site } from "@wix/site";
import { createClient } from "@wix/sdk";
import { seo } from "@wix/site-seo"; // Frontend module of your choice
import { products } from '@wix/stores' // Any REST API module

const wixClient = createClient({
  auth: site.auth(),
  host: site.host({ applicationId: "<your_app_id>" }),
  modules: {
    seo,
    products,
  },
});

export const injectAccessTokenFunction = wixClient.auth.getAccessTokenInjector();

wixClient.products.queryProducts().find().then((productsResult) => {
  console.log('Products => ', productsResult)
});

client.seo.title().then((title) => {
  console.log("Site title:", title);
});

type="text/javascript" (Classic)

In a classic script (when no type is specified or text/javacsript is specified), you can create the Wix Client and use it directly in the script.

import { site } from "@wix/site";
import { createClient } from "@wix/sdk";
import { seo } from "@wix/site-seo"; // Frontend module of your choice
import { products } from '@wix/stores' // Any REST API module

const wixClient = createClient({
  auth: site.auth(),
  host: site.host({ applicationId: "<your_app_id>" }),
  modules: {
    seo,
    products,
  });

wixClient.products.queryProducts().find().then((productsResult) => {
  console.log('Products => ', productsResult)
});

client.seo.title().then((title) => {
  console.log("Site title:", title);
});

Usage in Custom Elements

You can create a Wix Client in one of your custom elements when you create a Custom Element extension.

Create a Wix Client in your custom element

In a custom element, the custom element should expose an accessTokenListener property which is a function that will be called by Wix to inject the access token into the custom element. The property should be set to the return of getAccessTokenInjector on the auth object of the Wix Client.

import { site } from "@wix/site";
import { createClient } from "@wix/sdk";
import { seo } from "@wix/site-seo"; // Frontend module of your choice
import { products } from "@wix/stores"; // Any REST API module

const wixClient = createClient({
  auth: site.auth(),
  host: site.host({ applicationId: "<your_app_id>" }),
  modules: {
    seo,
    products,
  },
});

class MyCustomEleemnt extends HTMLElement {
  constructor() {
    super();
    this.accessTokenListener = wixClient.auth.getAccessTokenInjector();
  }
  connectedCallback() {
    wixClient.products
      .queryProducts()
      .find()
      .then((productsResult) => {
        console.log("Products => ", productsResult);
      });

    client.seo.title().then((title) => {
      console.log("Site title:", title);
    });
  }
}
customElements.define(tagName, MyCustomEleemnt);