Anura SDK for Node.js
The Anura SDK for Node.js simplifies using Anura Direct for developers. Allowing you to quickly get up and running with analyzing your traffic.
Getting Started
- Have an open active account with Anura - You can see more about Anura's offerings here.
- Minimum Requirements - To use the SDK, you will need Node >=18.
-
Install the SDK - You can run the following command to install the SDK via npm:
npm install @anuraio/anura-sdk
- View our Quick Examples to immediately begin using the SDK!
Quick Examples
Create the Anura Direct Client
import { AnuraDirect, AnuraClientError, AnuraServerError } from '@anuraio/anura-sdk'; // ES Module Import
const { AnuraDirect, AnuraClientError, AnuraServerError } = require('@anuraio/anura-sdk'); // CommonJS Import
const direct = new AnuraDirect('your-instance-id-value-here');
Set additional data for Anura Direct
/**
* To send additional data to Anura Direct, use the AdditionalData class.
* We will provide this object when calling direct.getResult()
*/
const additionalData = new AdditionalData();
additionalData.addElement(1, 'your-data-value');
Update additional data at a specific index
// To update an element of additional data, simply add the element again but with a new value.
const indexToUpdate = 1;
additionalData.addElement(indexToUpdate, 'your-new-data-value');
Remove an element from additional data
const indexToRemove = 1;
additionalData.removeElement(indexToRemove);
Get a result from Anura Direct
(async function() {
const direct = new AnuraDirect('your-instance-id');
try {
const result = await direct.getResult({
ipAddress: 'visitors-ip-address', // required
userAgent: 'visitors-user-agent', // optional
app: 'visitors-app-package-id', // optional
device: 'visitors-device-id', // optional
source: 'your-source-value', // optional
campaign: 'your-campaign-value', // optional
additionalData: additionalData, // optional
});
console.log(result);
} catch (error) {
if (error instanceof AnuraClientError) {
// Handle 4XX responses here.
} else if (error instanceof AnuraServerError) {
// Handle 5XX responses here.
} else {
// Handle any other type of error thrown here.
}
}
})();
API Reference
AnuraDirect
Can get results from Anura Direct. These results are fetched using /direct.json API endpoint.
Methods
async getResult(options: GetResultOptions): Promise<DirectResult>- Gets a result from Anura Direct. Throws an error if one occurs throughout the result fetching process.
- Error thrown:
- AnuraClientError: if a 4XX response is returned from Anura Direct
- AnuraServerError: if a 5XX response is returned from Anura Direct
- AnuraError: general exception that represents any other type of error that occurred while fetching from Anura Direct.
GetResultOptions Parameters:
Name | Type | Description | Required |
---|---|---|---|
ipAddress | string | The IP address of your visitor. Both IPv4 & IPv6 addresses are supported. | Yes |
userAgent | string|null | The user agent string of your visitor. | |
app | string|null | The application package identifier of your visitor (when available.) | |
device | string|null | The device identifier of your visitor (when available.) | |
source | string|null | A variable, declared by you, to identify "source" traffic within Anura's dashboard interface. | |
campaign | string|null | A subset variable of "source," declared by you, to identify "campaign" traffic within Anura's dashboard interface. | |
additionalData | AdditionalData|null | Additional Data gives you the ability to pass in select points of data with your direct requests, essentially turning Anura into "your database for transactional data." |
- Returns the instance you have set within your AnuraDirect client.
- Sets the instance of your AnuraDirect client to the instance value passed.
- Returns whether you're currently using HTTPS when calling the Anura Direct API. If false, you are using HTTP instead.
- Sets whether to use HTTPS or HTTP according to the useHttps value passed.
Additional Data
An object used for sending additional data to Anura Direct.
Methods
addElement(key: number, value: string): void- Adds an element of data to your AdditionalData.
- Removes the element of data located at the provided key from your AdditionalData.
- Returns the number of elements currently set within your AdditionalData.
- Returns your AdditionalData as a JSON string.
DirectResult
The result upon a successful call to getResult() from the AnuraDirect client. It contains not only the result from Anura DIrect, but some other methods to help you use the result as well.
Methods
iSuspect(): boolean- Returns whether or not the visitor has been determined to be suspect.
- Returns whether or not the visitor has been determined to be non-suspect.
- Returns whether or not the visitor has been determined to be on a mobile device.
- Returns the raw result string value.
- Returns the raw mobile result value.
- If you have return rule sets enabled, you will be able to see which specific rules were violated upon a suspect result. This value will be null if the visitor has non-suspect, or if you do not have return rule sets enabled.
- You can talk to support about enabling or disabling the return rule sets feature.
- If you have return invalid traffic type enabled, you will be able to access which type of occcurred upon a suspect result.
- You can talk to support about enabling or disabling the return invalid traffic type feature.