Skip to main content

Analyze Your Data

This guide will show you how to write custom code to analyze your data without touching it.

Key concepts in this guide:

Getting Started

To get started, you will need a Basis Theory account.

Next you will need a Management Application in order to provision the components in this guide.

Click here to create a Management Application or login to your Basis Theory account and create a new application from the Full Management Access template.

Save the API Key from the created Management Application as it will be used in this guide to provision the reactor.

Create a Reactor

Reactors provide a secure Node.js 16 runtime environment to be able to execute custom code. Reactors consist of two parts. The Reactor Formula is a re-usable template that can be used to create multiple Reactors.

First, we want to write some code that will take in a list of users and return the average age of all of the users:

module.exports = async function (req) {
// 3.15576e+10 is number of milliseconds in a year
const ages = req.args.map(user =>
Math.floor((new Date() - new Date(user.date_of_birth).getTime()) / 3.15576e+10));
const averageAge = ages.reduce((a, b) => a + b) / ages.length;

return {
raw: {
averageAge
}
};
};

Let's store the JavaScript code as a variable. In your terminal, run the following:

javascript='module.exports = async function (req) {
// 3.15576e+10 is number of milliseconds in a year
const ages = req.args.map(user => Math.floor((new Date() - new Date(user.date_of_birth).getTime()) / 3.15576e+10));
const averageAge = ages.reduce((a, b) => a + b) / ages.length;

return {
raw: {
averageAge
}
};
};'

Now, let's create a Reactor Formula with the variable we created:

curl "https://api.basistheory.com/reactor-formulas" \
-H "BT-API-KEY: test_1234567890" \
-H "Content-Type: application/json" \
-X "POST" \
-d '{
"name": "Average User Reactor",
"description": "Compute average age of users",
"type": "private",
"code": '"$(echo $javascript | jq -Rsa .)"'
}'
Be sure to replace test_1234567890 with the Management API Key you created in the Getting Started.

Finally, we need to create a Reactor from the formula we just created:

curl "https://api.basistheory.com/reactors" \
-H "BT-API-KEY: test_1234567890" \
-H "Content-Type: application/json" \
-X "POST" \
-d '{
"name": "Average User Reactor",
"formula": {
"id": "17069df1-80f4-439e-86a7-4121863e4678"
}
}'
Be sure to replace test_1234567890 with the Management API Key you created in the Getting Started step and replace 17069df1-80f4-439e-86a7-4121863e4678 with the id of the Reactor Formula you created.
Save the Reactor id from the response as it will be used to invoke the reactor.

Create a Private Application

We need a Private Application to create tokens and invoke our reactor:

curl "https://api.basistheory.com/applications" \
-H "BT-API-KEY: test_1234567890" \
-H "Content-Type: application/json" \
-X "POST" \
-d '{
"name": "Analyze Data App",
"type": "private",
"permissions": [
"token:create",
"token:use"
]
}'
Be sure to replace test_1234567890 with the Management API Key you created in the Getting Started step.
Save the API Key from the created Private Application as it will be used in this guide to create tokens and invoke our reactor.

Create Tokens

Let's create some tokens which will contain some user data including a date_of_birth property:

curl "https://api.basistheory.com/tokenize" \
-X "POST" \
-H "BT-API-KEY: key_9pz3kswmqTBYP2pzYiiCDM" \
-H "Content-Type: application/json" \
-d '[
{
"type": "token",
"data": {
"first_name": "Luke",
"last_name": "Skywalker",
"date_of_birth": "1978-03-22"
}
}, {
"type": "token",
"data": {
"first_name": "Han",
"last_name": "Solo",
"date_of_birth": "1981-11-04"
}
}, {
"type": "token",
"data": {
"first_name": "Leia",
"last_name": "Organa",
"date_of_birth": "1978-03-23"
}
}, {
"type": "token",
"data": {
"first_name": "Boba",
"last_name": "Fett",
"date_of_birth": "1984-08-14"
}
}, {
"type": "token",
"data": {
"first_name": "Chewbacca",
"last_name": "Wookie",
"date_of_birth": "1979-05-18"
}
}
]'
Be sure to replace test_1234567890 with the Private API Key you created in the Create a Private Application step.

Invoke the Reactor

Finally, we can invoke our reactor with the tokens we previously created. To do this, we will leverage Expressions to detokenize the request before passing the data directly into our code:

curl "https://api.basistheory.com/reactors/5b493235-6917-4307-906a-2cd6f1a90b13/react" \
-H "BT-API-KEY: test_1234567890" \
-H "Content-Type: application/json" \
-X "POST" \
-d '{
"args": [
"{{ ef5525ab-e0ba-45db-b4ca-38de730d5994 }}",
"{{ 446425d3-061b-408d-9d51-cfb4d049570e }}",
"{{ 5c347145-0ad3-4c38-b964-b0c4796a66c0 }}",
"{{ 0e11b9c8-576b-458d-908e-6489f27e3a97 }}",
"{{ 8391ba8b-2adb-4721-a88f-ae7a9e941b18 }}"
]
}'
Be sure to replace the following:

You should see the following JSON response:

{
"raw": {
"averageAge": 42
}
}

Conclusion

We were able to gather user insights about our data without directly touching the information, therefore reducing our risk and security scope.

You can perform advanced scenarios with Reactors by injecting a pre-configured instance of the Basis Theory JavaScript SDK when creating your Reactor. This can enable capabilities such as searching tokens or creating new tokens.

Learn More