> 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`). > - 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: About the Wix Client ## Article: About the Wix Client ## Article Link: https://dev.wix.com/docs/sdk/articles/set-up-a-client/about-the-wix-client.md ## Article Content: # About the Wix Client Use a Wix client to make authenticated calls to the [Wix JavaScript SDK](https://dev.wix.com/docs/sdk.md) from a self-hosted environment. ## When you need a Wix client You need to create a Wix client when developing the following: + [Self-hosted Wix apps](https://dev.wix.com/docs/build-apps/develop-your-app/frameworks/self-hosting/about-self-hosting-for-wix-apps.md) + [Self-managed headless projects](https://dev.wix.com/docs/go-headless/develop-your-project/about-headless-development-paths.md) ## When you don't need a Wix client You **don't** need to create a Wix client if you are working in the following Wix-hosted environments: + [Wix site development environments](https://dev.wix.com/docs/develop-websites/articles/get-started/development-environments.md) + [Wix CLI for Apps](https://dev.wix.com/docs/build-apps/develop-your-app/frameworks/wix-cli/about-the-wix-cli-for-apps.md) + [Wix Blocks](https://dev.wix.com/docs/build-apps/develop-your-app/frameworks/wix-blocks/about-wix-blocks.md) + [Wix CLI for Headless](https://dev.wix.com/docs/go-headless/cli-for-headless/about-the-wix-cli-for-headless.md) In these cases you can call the JavaScript SDK's APIs directly and authentication is taken care of for you automatically. ## 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) function and provide values for the following parameters: - `auth`: The [authorization strategy](#authorization-strategy-auth) used to authorize your calls to the SDK. - `host`: The [host](#hosted-context-host), when calling the SDK from a hosted context, such as the dashboard or editor. - `modules`: The [SDK modules](#sdk-modules-modules) you want to call using this client. Refer to the [examples](#examples) below to see how to create a client using these parameters for a number of different scenarios. ## Authorization strategy (`auth`) There are a number of different authorization strategies you can use with the `auth` parameter. The correct strategy to use depends on: - The [context](https://dev.wix.com/docs/sdk/articles/set-up-a-client/authorization-strategies.md) in which you are making API calls. For example, whether you are calling the SDK from a Headless site or a Wix app. - The [identity](https://dev.wix.com/docs/api-reference/articles/authentication/about-identities.md) you are using to call the API. For example, whether you are calling the SDK as a site visitor or as Wix app. Note that each function in the SDK can only be called by the identities specified in the SDK reference. The following table outlines the supported authorization strategies, the cases they apply to, and the parameters needed to create the strategies: | Auth strategy | Supported Contexts | Identities | Parameters | | ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [`OAuthStrategy`](https://dev.wix.com/docs/sdk/core-modules/sdk/oauth-strategy.md) | Headless site or app | Site visitor, site member | Client ID, Tokens (when [resuming a session](https://dev.wix.com/docs/go-headless/develop-your-project/self-managed-headless/authentication/visitors/handle-visitors-using-the-js-sdk.md)) | | [`AppStrategy`](https://dev.wix.com/docs/sdk/core-modules/sdk/app-strategy.md) | Wix app ([backend extensions](https://dev.wix.com/docs/build-apps/develop-your-app/extensions/about-extensions.md#backend-extensions)) | Wix app, Wix user (when using [elevation](https://dev.wix.com/docs/api-reference/articles/authentication/about-elevated-permissions.md)), site visitor or site member (when passing an access token from the frontend) | app ID, app secret, app instance ID (when authenticating with the Wix app identity), App public key (when using webhooks or [service plugins](https://dev.wix.com/docs/build-apps/develop-your-app/extensions/backend-extensions/service-plugins/about-service-plugin-extensions.md)), Tokens (when authenticating as a site visitor or site member) | | [`ApiKeyStrategy`](https://dev.wix.com/docs/sdk/core-modules/sdk/api-key-strategy.md) | Headless site or app, Wix app | API key | API Key | | [Host module auth](#host-module-auth) | Wix app ([frontend extensions](https://dev.wix.com/docs/build-apps/develop-your-app/extensions/about-extensions.md#frontend-extensions)) | Site visitor, site member, Wix user | - | ### Host module auth When creating a client within a [hosted context](#hosted-context-host), such as the dashboard or the editor, use the host's specific authorization strategy by calling the host module's `auth()` function. For example, `dashboard.auth()`. If you are only calling frontend modules from the editor or site, you can omit the authentication altogether. The identity used by a client created with a host authorization strategy depends on which host is being used. Within the dashboard, the identity is Wix user. In the editor or site, the identity is site visitor or site member. ## Hosted context (`host`) Depending on the context in which you are making API calls, you may need to specify a `host`. - In [self-hosted apps](https://dev.wix.com/docs/build-apps/develop-your-app/frameworks/self-hosting/about-self-hosting-for-wix-apps.md): - When working in a hosted context, such as the dashboard or editor, use the relevant host module's `host()` function to specify the host when creating a client. - When working in a non-hosted context, such as your app backend, do not 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 will call using that client. Simply list the imported modules you want to call. ## Examples ### Headless as a visitor Create a client to call Bookings Services APIs as a site visitor using the `OAuthStrategy` for a Headless site or app: ```js import { createClient, OAuthStrategy } from "@wix/sdk"; import { services } from "@wix/bookings"; //... const myWixClient = createClient({ auth: OAuthStrategy({ clientId: "", }), modules: { services } }); //... const { items } = await myWixClient.services.queryServices({}); ``` ### Wix app as a Wix app 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: "", appSecret: "", instanceId: "" }), modules: { services } }); //... const { items } = await myWixClient.bookings.queryBookings({}); ``` ### 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({}); 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({}); 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', async (req, res) => { const accessToken = req.headers['Authorization']; const myWixClient = createClient({ auth: AppStrategy({ appId: "", appSecret: "", accessToken }), modules: { services } }); const { items } = await myWixClient.bookings.queryBookings({}); res.send({}); }); ``` ### Headless or Wix app using an API Key Create a client to call Bookings Services APIs using the `ApiKeyStrategy` for a Headless site or Wix app: ```js import { createClient, ApiKeyStrategy } from "@wix/sdk"; import { services } from "@wix/bookings"; //... const myWixClient = createClient({ auth: ApiKeyStrategy({ apiKey: "", }), modules: { services } }); //... const { items } = await myWixClient.services.queryServices({}); ```