Create a Client with an API Key

Share your feedback
Reach out to us with feedback and suggestions to improve the Wix Headless experience, and join the Headless channel of the Devs on Wix Discord community to discuss features and connect with our growing community of developers.

Once you have generated an API key and obtained the IDs for your Wix account and Wix site, you can use them to create an SDK client that uses your API key to authenticate.

Prerequisites

Note: Before you start, make sure you are using Node.js version 18 or higher.

Install packages

Install @wix/sdk and the packages for the domain-specific APIs you wish to work with. Domain-specific packages follow the naming convention @wix/{domain}. For example:

Copy
1
npm install @wix/sdk
2
npm install @wix/stores

or

Copy
1
yarn add @wix/sdk
2
yarn add @wix/stores

Import modules

To use the APIs you have installed, import createClient from the @wix/sdk package along with an authentication strategy, and import functionality from the desired domain-specific packages you installed. For example:

Copy
1
import { createClient, ApiKeyStrategy } from '@wix/sdk';
2
import { contacts } from '@wix/contacts';
3
import { collections, products } from '@wix/stores';

Create a client

To get started connecting an external client with a Wix site, use the createClient() function imported from the @wix/sdk package.

Copy
1
const myWixClient = createClient({
2
auth: ApiKeyStrategy({
3
apiKey: '<API_KEY>',
4
siteId: '<SITE_ID>',
5
accountId: '<ACCOUNT_ID>',
6
}),
7
modules: {
8
contacts: contacts.contacts,
9
products: stores.products,
10
},
11
});

The createClient() function returns a new Wix client.

  • The modules parameter property contains key:value pairs. Each value is an imported module you wish to enable in the client, and the key defines the name to use for working with that module.
  • The auth parameter property contains the authentication method and credentials for connecting with your Wix project. To create a client for your Wix project, pass ApiKeyStrategy() with an object containing:
    • apiKey: An API key that you generated in the API Key Manager.
    • siteId: ID of the Wix site or project you are working with. Required for site-level API calls.
    • accountId: ID of the Wix account the API key belongs to. Required for account-level API calls.

Once you have created a client successfully, you can begin using it to call functions in the API Reference.

Example

Copy
1
import { createClient } from '@wix/sdk';
2
import { products } from '@wix/stores';
3
4
const myWixClient = createClient({
5
auth: ApiKeyStrategy({
6
apiKey: '<API_KEY>',
7
siteId: '<SITE_ID>',
8
accountId: '<ACCOUNT_ID>',
9
}),
10
modules: {
11
cart,
12
products,
13
},
14
});
15
16
// ...
17
18
const { items } = await myWixClient.products
19
.queryProducts()
20
.startsWith('name', 'shoes')
21
.limit(4)
22
.find();
Was this helpful?
Yes
No