ApiKeyStrategy
Important: This strategy is for administrative operations at the site or account level. For security purposes, a request made from the browser using an API key will encounter errors attributed to cross-origin resource sharing (CORS) restrictions. To learn more, see Authorization Strategies.
This authentication strategy is used along with a Wix Client
to authenticate API calls using an API Key.
When using this authentication strategy for an API request, the requester's identity is that of the corresponding API Key. The permissions associated with this API Key are set in the Dev Center.
ApiKeyStrategy()
Creates an authentication strategy object that uses an API key.
Syntax
1ApiKeyStrategy({apiKey: string, accountId: string, siteId: string}): ApiKeyStrategy
Parameters
Name | Type | Description |
---|---|---|
apiKey | string | The API key to use for authentication. |
accountId | string | Optional. The account ID to use for authentication when calling account-level APIs. |
siteId | string | Optional. The site ID to use for authentication when calling site-level APIs. |
Note:
You must provide either a siteId
or accountId
value, depending on whether you're calling site-level or account-level functionality. To learn how to retrieve these values, see Site and Account IDs.
Returns
ApiKeyStrategy
Functions
setAccountId()
Sets the account ID to use along with an API Key when authenticating.
Use an account ID for authentication when you need to call account-level APIs.
Set the accountId
to undefined
and use the setSiteId()
function to set a siteId
if you want to use an API key to call site-level APIs.
Syntax
1setAccountId(accountId: string): void
Parameters
Name | Type | Description |
---|---|---|
accountId | string | ID of the Wix account to use when authenticating. |
setSiteId()
Sets the site ID to use along with an API Key when authenticating.
Use a site ID for authentication when you need to call site-level APIs.
Set the siteId
to undefined
and use the setAccountId()
function to set an accountId
if you want to use an API key to call account-level APIs.
Syntax
1setSiteId(siteId: string): void
Parameters
Name | Type | Description |
---|---|---|
siteId | string | ID of the Wix account to use when authenticating. |
Example
The following code creates a client that can make requests to the coupons module using the API key strategy. The site ID is for providing site-level operations. To perform account-level operations, include the account ID instead of the site ID.
1import { createClient, ApiKeyStrategy } from '@wix/sdk';2import { coupons } from "@wix/marketing";3
4const myClient = createClient({5 modules: { coupons },6 auth: ApiKeyStrategy({7 apiKey: '<API_KEY>',8 siteId: '<SITE_ID>'9 })10});11
12const couponId = "058b0b56-e90d-4f4e-a8a3-8bf90b3fc4e6"13const response = await wixClient.coupons.deleteCoupon(couponId);