Make authenticated requests using Axios

Axios is a popular promise-based HTTP client. It can be used to transform and intercept HTTP request and response data asynchronously.

This tutorial shows you how to make authenticated requests using Axios. You can use Axios interceptors to append an authenticated session token header prior to each request.

Requirements

Configure your Axios interceptor

  1. Install an interceptor to your instance of Axios.
  2. Define a function that your Axios instance calls prior to each request.

    The function takes a config object as parameter and returns a config object that's used as configuration for all subsequent requests.

  3. In the function, call getSessionToken() and resolve the token it returns.

  4. Modify the config object to append an “Authorization”: “Bearer token” header to your requests, like in the following example:

Next steps

  • Learn how to use helper functions to fetch a session token from Shopify App Bridge and include them in requests being made to the app backend.
  • Convert a multi-page, server-side rendered (SSR) app to use Shopify App Bridge authentication with Turbolinks.
  • Learn how to build a Shopify app with Rails 6, React, and Shopify App Bridge authentication.