Wix Client
A Wix Client is used to call functionality from the Wix JavaScript SDK. APIs from the SDK's modules, such as bookings
and ecom
, are called using the API Client. The client also manages the authentication of SDK calls.
createClient()
Creates a client instance with a designated authentication strategy, such as OAuthStrategy
, AppOAuthStrategy
, or ApiKeyStrategy
. For guidance on selecting the appropriate strategy, see Authorization Strategies.
Syntax
1createClient(config: {modules: object, auth: AuthenticationStrategy, headers: Headers, host: Host}): WixClient
Parameters
Name | Type | Description |
---|---|---|
config | ClientConfig | Client configuration. |
ClientConfig
Name | Type | Description |
---|---|---|
modules | object | SDK modules to initialize for use with this client. |
auth | AuthenticationStrategy | Authentication strategy to use for authenticating API calls, such as OAuthStrategy , AppOAuthStrategy , or APIKeyStrategy . |
headers | Headers | Headers for client calls. Can be used to set alternative authentication credentials such as authorization tokens used when creating a client to be used in a Wix App. |
host | Host | An API host. You only need to set a host when using a client to call hosted modules, such as the dashboard module. |
Returns
WixClient
Functions
The following functions are available once you have a WixClient
instance.
fetch()
Fetch a resource from the Wix REST API using the client's authentication.
Note: Only use this function if the functionality you're looking for doesn't yet exist in the JavaScript SDK. Otherwise, use the SDK version.
Syntax
1fetch(relativeUrl: string, options: RequestInit): Promise<Response>
Parameters
Name | Type | Description |
---|---|---|
relativeUrl | string | URL of the Wix REST API to call. |
options | RequestInit | Request options. |
Returns
Response - Standard fetch()
response.
setHeaders()
Sets headers for the client.
You can use the setHeaders()
function to set headers such as an authorization header to be used for authentication when using the Wix JavaScript SDK from a Wix App.
Syntax
1setHeaders(headers: Record<string, string>): void
Parameters
Name | Type | Description |
---|---|---|
headers | Record<string, string> | Header information. |
use()
Binds an SDK module with the client's authentication strategy and returns an initialized module.
Syntax
1use(module: Module): InitializedModule
Parameters
Name | Type | Description |
---|---|---|
modules | Module | An SDK module to bind with the client's authentication strategy. |
Returns
InitializedModule
- An initialized module bound to the client's authentication strategy.
Example
1import stores from '@wix/stores';2
3// ...4
5const store = myWixClient.use(stores);6
7const { items } = await store.products.queryProducts().find();
graphql()
Executes a GraphQL query against the Wix GraphQL API.
Syntax
1graphql<Result, Variables>(2 query:3 | string4 | TypedQueryInput<Result, Variables>,5 variables?: Variables,6 opts: {7 apiVersion: string;8 } = {9 apiVersion: 'alpha',10 },11 ): Promise<{12 data: Result;13 errors?: GraphQLFormattedError[];14 }>
Parameters
Name | Type | Description |
---|---|---|
query | TypedQueryInput<Result, Variables> | A graphql query to execute. Supports query strings decorated with type information, for Result and Variables type inference. |
variables | Variables | The variables to use in the query. |
opts.apiVersion | string | The version of the Wix GraphQL API to use (default: 'alpha'). |
Returns
Promise<{ data: Result; errors?: GraphQLFormattedError[]; }>
- A promise with data
as the Result
of the query, and optionally errors
with a list of errors while executing the query.
Example
1const productsQuery = graphql(`2 query Products($filter: JSON!) {3 storesProductsV1Products(queryInput: { query: { filter: $filter }}) {4 items {5 name6 }7 }8 }9 `);10
11 const { data } = await myWixClient.graphql(productsQuery, {12 filter: {13 name: {14 $startsWith: query || ''15 }16 },17 });
Properties
auth
Gets the client's authentication strategy.
Use the auth
property to work with the client's authentication strategy, such as to get tokens when using the OAuthStrategy
or to set IDs when using the ApiKeyStrategy
.
Type
AuthorizationStrategy
- Either OAuthStrategy
or ApiKeyStrategy
.