Track Usage with Auth0

*Example with NextJS

Overview

Step 1: Get your Auth0 Management API Access Token

Step 2: Create an API Route Handler

Step 3: Track user's usage in your app using the API

Step 1: Get your Auth0 Management API Access Token

Follow the official Auth0 guide on how to retrieve an Auth0 Management API Access Token:

circle-exclamation
circle-info

This part corresponds to the highlighted code chunk in Step 2.

Make sure to select all scopes: read:users, read:user_idp_tokens update:users update:users_app_metadata
triangle-exclamation

Step 2: Create an API Route Handler

Configure .env file

Add these 3 variables to your .env file.

circle-exclamation

Configure API route

Copy and paste the following file into your app/api/ARES/route.ts file.

This route will have a GET method and a POST method.

triangle-exclamation

GET Method (Access User Information)

Calling GET Method

With a user signed in with ARES, you can call the API endpoint you just configured to access the user's data from ARES's server.

Example of getting the user's information through a NextJS client component:

GET Response

A GET request to your /api/ARES endpoint should return the user's information in json format, including the number of credits the user has left.

Step 3: Track user's usage in your app using the API

POST Method (Logs and Tracks User's Usage to Earn Credits)

circle-exclamation

Calling POST Method

circle-info

Make sure to call this method and check the response success message BEFORE you let your user use the service. This message logs the usage on ARES's server, keeping track of the ARES credits you've earned. Once you have called this method and receive a success message, allow the user to continue.

triangle-exclamation

Here's an example of a React button that earns 1 ARES credit for your app (and deducts the credit from the user's account).

Last updated