Create a Client with OAuth

Share your feedback
Reach out to us with feedback and suggestions to improve the Wix Headless experience, and join the Headless channel of the Devs on Wix Discord community to discuss features and connect with our growing community of developers.

After you create an OAuth app for your client site or app in your project dashboard, you can begin coding by installing the Wix JavaScript SDK, importing modules in your code, and creating an SDK client.

Prerequisites

Note: Before you start, make sure you are using Node.js version 18 or higher.

Install packages

Install @wix/sdk and the packages for the domain-specific APIs you wish to work with. Domain-specific packages follow the naming convention @wix/{domain}. For example:

コピー
1

または

コピー
1

Import modules in your code

To use the APIs you have installed, import createClient より @wix/sdk package along with an authentication strategy, and import functionality from the desired domain-specific packages you installed. For example:

コピー
1

Make sure to import both createClient そして OAuthStrategy from @wix/sdk.

Create a client

To get started connecting to a Wix project, use the createClient() function imported from the @wix/sdk package to create a new client.

例えば、こうだ:

コピー
1

Pass the modules you want to use and authorization information when creating the client.

  • について modules property contains key:value pairs. Each value is an imported module you wish to enable in the client, and the key defines the name to use for working with that module.
  • について auth property contains the authentication method and credentials for connecting with your Wix project. To use the OAuth authentication strategy, call OAuthStrategy() with an object containing:
    • clientId: Your OAuth app client ID. You can retrieve it from the Headless Settings menu in the dashboard. If you haven't created an OAuth app yet, see Create an OAuth App to set one up.
    • tokens: Optional. An object containing access and refresh tokens for a site visitor or member. If you pass tokens, every call made by the client uses these tokens to maintain a persistent session.

Call APIs with a client

After creating a client, you can use it to call APIs.

例えば、こうだ:

コピー
1

Work with visitor or member sessions

Often, you will need to maintain a persistent session to work with site or app visitors and members. Sessions are maintained using tokens. Most of the token management is handled by the client object, but there is some work you need to do, which differs depending on your situation.

役に立ちましたか?
はい
いいえ