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

Copy
1
ApiKeyStrategy({apiKey: string, accountId: string, siteId: string}): ApiKeyStrategy

Parameters

NameTypeDescription
apiKeystringThe API key to use for authentication.
accountIdstringOptional. The account ID to use for authentication when calling account-level APIs.
siteIdstringOptional. 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

Copy
1
setAccountId(accountId: string): void

Parameters

NameTypeDescription
accountIdstringID 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

Copy
1
setSiteId(siteId: string): void

Parameters

NameTypeDescription
siteIdstringID 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.

Copy
1
import { createClient, ApiKeyStrategy } from '@wix/sdk';
2
import { coupons } from "@wix/marketing";
3
4
const myClient = createClient({
5
modules: { coupons },
6
auth: ApiKeyStrategy({
7
apiKey: '<API_KEY>',
8
siteId: '<SITE_ID>'
9
})
10
});
11
12
const couponId = "058b0b56-e90d-4f4e-a8a3-8bf90b3fc4e6"
13
const response = await wixClient.coupons.deleteCoupon(couponId);
Was this helpful?
Yes
No