When following the guide, select the application with these scopes: read:users, read:user_idp_tokensupdate:users update:users_app_metadata
This part corresponds to the highlighted code chunk in Step 2.
Make sure to select all scopes: read:users, read:user_idp_tokensupdate:users update:users_app_metadata
Don't expose this token to the client-side of your application!
Then, you can make an HTTP GET call to the Get a User endpoint to retrieve the tokens. We'll do this in the route handler in Step 2.
Step 2: Create an API Route Handler
Configure .env file
Add these 3 variables to your .env file.
Change all values to your own app's values.
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.
Always keep this route server-side only.
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)
IMPORTANT: If you check whether or not a user has a subscription before allowing them to use a certain feature on your app, make sure to disable that for users signed in with ARES in order for them to be able to pay-per-use with their ARES credits without having a subscription to your service.
Calling POST Method
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.
Only call the API for users who signed in with ARES, otherwise the API will return an error.
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).
"use client";
import React from "react";
const handleAresUsageUpdate = async () => {
try {
// Make sure to configure this API route first (at the beginning of this page).
const response = await fetch("/api/ARES", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
usage: "test-button", // Replace with actual usage reason. Must be consistent across usages. (e.g. every time the user uses the test button, use value "test-button", every time the user uses a different button, use value "different-button".
credits: 1, // Replace with actual credits value
}),
});
if (!response.ok) {
throw new Error("Failed to update ARES usage");
}
const data = await response.json();
} catch (error) {
}
};
const UsageButton = () => {
return (
<button
onClick={handleAresUsageUpdate}
>
Update Usage
</button>
);
};
export default UsageButton;