Create a Client with OAuth

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:

Copy

or

Copy

Import modules in your code

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

Copy

Make sure to import both createClient and 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.

For example:

Copy

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

  • The 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.
  • The 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.

For example:

Copy

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.

Did this help?