Elevate API Call Permissions with the CLI

In workflows that use mostly site visitor, site member, or Wix user authentication, you may occasionally need to make calls with elevated permissions. You can use the JavaScript SDK to provide specific calls with Wix app authentication.

The process involves 2 steps:

  1. Set up your app's backend code to handle elevated requests.
  2. Send an authenticated call from your frontend code to your app's backend. Frontend code includes site extension and dashboard extension code.

You can make calls from your frontend code to your app's backend using either web method extensions or API extensions. We recommend using web method extensions in this situation as they offer several advantages over API extensions.

Important: Exposed elevated function calls create a security risk for privilege escalation attacks. Make sure to protect your exposed function calls with the appropriate logic.

Elevating permissions using a web method extension

To elevate permissions for calls to Wix APIs using a web method extension:

Step 1 | Set up the web method in your app's backend

Set up your app's backend to handle requests for elevated function calls from your frontend.

To set up your backend:

  1. Create a web method extension to to define a function in your app's backend that you can call from your frontend code.

  2. In the extension's web.ts file in your CLI project, import the auth submodule from @wix/essentials as well as the module containing the function that you want to make elevated calls to.

    Copy
  3. Define a web method that calls the function you need, using the permissions parameter to define the required permissions.

Wrap the function with auth.elevate() before calling it.

Copy

Step 2 | Call the web method from your frontend

To call the web method, import it from the extension's web.ts file, then call it in your code.

Copy

This call is authenticated automatically.

Elevating permissions using an API extension

To elevate permissions for calls to Wix APIs using an API extension:

Step 1 | Set up the API extension in your app's backend

Set up your app's backend to handle requests for elevated function calls from your frontend.

To set up your backend:

  1. Create an API extension to allow your app to expose backend HTTP functions.
  2. In the api.ts file in your CLI project, import the auth submodule from @wix/essentials as well as the module containing the function that you want to make elevated calls to.
    Copy
  3. Expose an endpoint that calls the function you need. Wrap the function with auth.elevate() before calling it.
    Copy

Step 2 | Send authenticated requests from your frontend

Send authenticated requests from your site's frontend code to your backend endpoint.

To send requests:

  1. Import the httpClient submodule from the @wix/essentials package.
    This submodule includes a function called fetchWithAuth. This function automatically signs API calls with an authorization header that identifies the current site visitor, site member, or Wix user.
    Copy
  2. Call your app's backend HTTP function using fetchWithAuth().
    The base URL for your endpoint is provided automatically by the CLI. Note that the path for your endpoint is based on the name of its containing folder in the CLI.
    Copy

See also

Did this help?