> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

## Resource: Set Up a Wix Client

## Article: Set Up a Wix Client

## Article Link: https://dev.wix.com/docs/api-reference/articles/sdk-setup-and-usage/set-up-a-wix-client.md

## Article Content:

<!--Not sure about this article. Where it should be in the menu. Whether it should be in the reference at all. Maybe we should just have a general auth article that points to other places in DW, BAP, and HL-->

# Set Up a Wix Client

A Wix client is an object you create in your code to make authenticated calls to the Wix JavaScript SDK from self-managed environments, such as self-managed apps or headless projects. The client manages authentication and provides access to the SDK modules you specify.

## When you need a client

You need to create a Wix client in these environments:

- [Self-managed Wix apps](https://dev.wix.com/docs/build-apps/develop-your-app/develop-a-self-managed-app/about-self-managed-apps.md)
- [Self-managed headless projects](https://dev.wix.com/docs/go-headless/get-started/about-wix-headless.md)
- Wix-managed headless projects that bring their own frontend instead of using Wix's Astro integration, such as a Claude Design build or an existing SPA

You **don't** need to create a client if you are developing in these environments:

- [Wix sites](https://dev.wix.com/docs/develop-websites.md)
- [Wix CLI apps](https://dev.wix.com/docs/build-apps/develop-your-app/develop-an-app-with-the-cli/about-the-wix-cli.md)
- [Wix Blocks apps](https://dev.wix.com/docs/build-apps/develop-your-app/develop-an-app-with-blocks/about-wix-blocks.md)
- [Wix-managed headless projects that use Wix's Astro integration](https://dev.wix.com/docs/go-headless/wix-managed-headless/about-the-astro-integration.md)

In these cases, you can call the SDK's APIs directly and authentication is handled automatically.

> **Note:** In Wix-managed headless, Wix hosts your frontend, but hosting alone doesn't handle authentication. Only the Astro integration authenticates for you. If you bring your own frontend, you must set up authentication yourself with a Wix client.

## Create a client

In cases where you need to create a client, call the [`createClient()`](https://dev.wix.com/docs/sdk/core-modules/sdk/wix-client.md) method and provide values for:

- `auth`: The [authorization strategy](#authorization-strategies-auth) for your calls.
- `host`: The [host](#hosted-context-host), which is only needed in some hosted contexts, like dashboard or editor.
- `modules`: The [SDK modules](#sdk-modules-modules) you want to use.

Refer to the [examples](#code-examples) below to see how to create a client using these parameters for a number of different scenarios.

## Authorization strategies (`auth`)

The correct strategy depends on your context and the identity you want to use. To learn more about identities, see [About Identities](https://dev.wix.com/docs/api-reference/articles/authentication/about-identities.md).

The following strategies are available:

- **AppStrategy:** For Wix apps. Requires app credentials and [instance ID](https://dev.wix.com/docs/build-apps/develop-your-app/access/app-instances/about-app-instances.md).
- **OAuthStrategy:** For visitors or members in headless projects. Requires a [client ID](https://dev.wix.com/docs/sdk/core-modules/sdk/oauth-strategy.md) and tokens.
- **ApiKeyStrategy:** Use for admin access in headless, channel, or enterprise contexts. Requires an [API key](https://dev.wix.com/docs/rest/articles/get-started/api-keys.md) and site or account ID.
- **Host module auth:** For dashboard, editor, and site pages in Wix apps. Auth is handled by the host module.

The strategy you should use depends on your context. The following table outlines the supported authorization strategies, the cases they apply to, and the parameters needed to create the strategies:

| Context                     | Identity             | Strategy                                                                             |
| --------------------------- | -------------------- | ------------------------------------------------------------------------------------ |
| Wix app (backend extension) | App, Visitor, Member | [AppStrategy](https://dev.wix.com/docs/sdk/core-modules/sdk/app-strategy.md)            |
| Wix app (dashboard page)    | Wix user             | [Host module auth](https://dev.wix.com/docs/sdk/host-modules/dashboard/introduction.md) |
| Wix app (editor/site page)  | Visitor, Member      | [Host module auth](https://dev.wix.com/docs/sdk/host-modules/editor/introduction.md)    |
| Headless site or app        | Visitor, Member      | [OAuthStrategy](https://dev.wix.com/docs/sdk/core-modules/sdk/oauth-strategy.md)        |
| Headless site or app        | Admin                | [ApiKeyStrategy](https://dev.wix.com/docs/sdk/core-modules/sdk/api-key-strategy.md)     |
| Wix Channel/Enterprise app  | Admin                | [ApiKeyStrategy](https://dev.wix.com/docs/sdk/core-modules/sdk/api-key-strategy.md)     |

Learn more about [Authorization Strategies](https://dev.wix.com/docs/sdk/articles/set-up-a-client/authorization-strategies.md).

## Hosted context (`host`)

Depending on the context in which you are making API calls, you may need to specify a `host`.

- In [self-managed apps](https://dev.wix.com/docs/build-apps/develop-your-app/develop-a-self-managed-app/about-self-managed-apps.md):
  - When working in a hosted context, such as the dashboard or editor, use the relevant host module's `host()` method to specify the host when creating a client.
  - When working in a non-hosted context, such as your app backend, don't specify a `host` when creating a client.
- In Headless sites and apps, you never need to specify a `host` when creating a client.
- In apps created with the Wix CLI, you don't use a client, so you never need to specify a `host`.

Each hosted context has its own host module. The host modules are:
- [`dashboard`](https://dev.wix.com/docs/sdk/host-modules/dashboard/introduction.md)
- [`editor`](https://dev.wix.com/docs/sdk/host-modules/editor/introduction.md)
- [`site`](https://dev.wix.com/docs/sdk/host-modules/site/introduction.md)

## SDK modules (`modules`)

In all cases, when creating a client, you need to specify which SDK modules you want to call using that client. Simply list the imported modules you want to call.

## Code examples

The following code examples show how to create a client in different contexts and with different authorization strategies.

### Headless as a visitor (`OAuthStrategy`)

Create a client to call Bookings Services APIs as a site visitor using the `OAuthStrategy` for a Headless site or app.

You can find your client ID in the [**Headless Settings**](https://www.wix.com/my-account/site-selector/?buttonText=Select%20Site&title=Select%20a%20Site&autoSelectOnSingleSite=true&actionUrl=https:%2F%2Fwww.wix.com%2Fdashboard%2F%7B%7BmetaSiteId%7D%7D%2Foauth-apps-settings) of your project dashboard.

```js
import { createClient, OAuthStrategy } from "@wix/sdk";
import { services } from "@wix/bookings";

//...

const myWixClient = createClient({
    auth: OAuthStrategy({
      clientId: "<YOUR_CLIENT_ID>",
    }),
    modules: {
      services
    }
});

//...

const { items } = await myWixClient.services.queryServices().find();
```

### Headless or Wix App as an admin (`ApiKeyStrategy`)

```js
import { createClient, ApiKeyStrategy } from "@wix/sdk";
import { services } from "@wix/bookings";

const wixClient = createClient({
  auth: ApiKeyStrategy({ 
    apiKey: "<YOUR_API_KEY>", 
    siteId: "<SITE_ID>" 
  }),
  modules: { services },
});

//...

const { items } = await myWixClient.services.queryServices().find();
```


### Wix app as a Wix app (`AppStrategy`)

Create a client to call Bookings Services APIs as the Wix app identity using the `AppStrategy` for a Wix app:

```js
import { createClient, AppStrategy } from "@wix/sdk";
import { services } from "@wix/bookings";

//...

const myWixClient = createClient({  
  auth: AppStrategy({
    appId: "<YOUR_APP_ID>",
    appSecret: "<YOUR_APP_SECRET>",
    instanceId: "<APP_INSTANCE_ID>"
  }),
  modules: {
    services
  }
});

//...

const { items } = await myWixClient.bookings.queryBookings().find();
```


### Wix app as a Wix user in the dashboard 

Create a client to call Bookings Services and Dashboard APIs as a Wix user using the dashboard host for a Wix app:

```js
import { createClient } from "@wix/sdk";
import { dashboard } from "@wix/dashboard";
import { services } from "@wix/bookings";

//...

const myWixClient = createClient({
  host: dashboard.host(),
  auth: dashboard.auth(),
  modules: {
    dashboard,
    services
  }
});

//...

const { items } = await myWixClient.bookings.queryBookings().find();

dashboard.showToast({
  message: "This is a toast!",
  type: "success",
});
```


### Wix app as a site visitor or member in the editor (or site)

Create a client to call Bookings Services and Editor APIs as a site visitor or member using the editor host for a Wix app:

```js
import { createClient } from "@wix/sdk";
import { editor, info } from "@wix/editor";
import { services } from "@wix/bookings";

//...

const myWixClient = createClient({
  host: editor.host(),
  auth: editor.auth(),
  modules: {
    info,
    services
  }
}); 

//...

const { items } = await myWixClient.bookings.queryBookings().find();

const languageCode = await client.info.getLanguageCode();
```

### Wix app as a visitor or member in the backend

Call Bookings Services APIs as a site visitor or member in the backend of a Wix app. To do so, you need to pass a visitor or member access token from the frontend:

```js
// Frontend code

const result = await fetchWithAuth('https://my-backend.com/api/func');
```

```js
// Backend code

import { services } from "@wix/bookings";
import { createClient, AppStrategy } from '@wix/sdk';

app.get('/func', (req, res) => {
  const accessToken = req.headers['Authorization'];

  const myWixClient = createClient({
    auth: AppStrategy({
      appId: "<YOUR_APP_ID>",
      appSecret: "<YOUR_APP_SECRET>",
      accessToken
    }),
    modules: {
      services
    }
  });

  
  const { items } = await myWixClient.bookings.queryBookings().find();

  res.send({});
});

```

## See also

- [About Identities](https://dev.wix.com/docs/api-reference/articles/authentication/about-identities.md)
- [Authorization Strategies](https://dev.wix.com/docs/sdk/articles/set-up-a-client/authorization-strategies.md)
- [API Keys](https://dev.wix.com/docs/api-reference/articles/authentication/about-api-keys.md)
- [Elevated Permissions](https://dev.wix.com/docs/api-reference/articles/authentication/about-elevated-permissions.md)