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.
Create a Reactor
Reactors provide a secure Node.js 16 runtime environment to be able to execute custom code.
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.15576e10));
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:
curl "https://api.basistheory.com/reactors" \
-H "BT-API-KEY: <MANAGEMENT_API_KEY>" \
-H "Content-Type: application/json" \
-X "POST" \
-d '{
"name": "Average User Reactor",
"code": '"$(echo $javascript | jq -Rsa .)"'
}'
<MANAGEMENT_API_KEY>
with the Management API Key you created in the Getting Started step.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: <MANAGEMENT_API_KEY>" \
-H "Content-Type: application/json" \
-X "POST" \
-d '{
"name": "Analyze Data App",
"type": "private",
"permissions": [
"token:create",
"token:use"
]
}'
<MANAGEMENT_API_KEY>
with the Management API Key you created in the Getting Started step.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: <PRIVATE_API_KEY>" \
-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"
}
}
]'
<PRIVATE_API_KEY>
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: <PRIVATE_API_KEY>" \
-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 }}"
]
}'
<PRIVATE_API_KEY>
with the Private API Key you created in the Create a Private Application step5b493235-6917-4307-906a-2cd6f1a90b13
with the Reactorid
you created in the Create a Reactor step- Token identifiers in the expressions with the tokens you created in the Create Tokens step
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.