snipe-node-sdk
v1.1.2
Published
Snipe SDK is a TypeScript library designed for interacting with the Snipe API. It provides a straightforward and efficient way to integrate various Snipe API functionalities into your application, including user signup, tracking events, managing token his
Downloads
20
Readme
Snipe SDK Node.js
Introduction
Snipe SDK is a TypeScript library designed for interacting with the Snipe API. It provides a straightforward and efficient way to integrate various Snipe API functionalities into your application, including user signup, tracking events, managing token history, and accessing coin data.
Installation
Before you can use the Snipe SDK, you need to install it in your project. You can do this using npm:
npm install snipe-node-sdk --save
Usage
To start using the Snipe SDK, import it into your project and initialize it with your API key.
Initialization
import { SnipeSdk } from "snipe-node-sdk";
const apiKey = "your_api_key";
const snipeSdk = new SnipeSdk(apiKey);
Example Usage
Sign Up
The signUp
function is used to register a new user. It now supports additional parameters for email and phone number along with the user hash.
Syntax
await snipeSdk.signUp(hash: string, email?: string, phone?: string, name?: string)
Parameters
hash
: A string representing the user hash. This parameter is required.email
: An optional string representing the user's email address.phone
: An optional string representing the user's phone number.
Return Value
This function returns a Promise
that resolves to a string. The string is a message indicating the result of the signup operation.
Example Usage
const hash = "user_hash";
const email = "[email protected]";
const phone = "1234567890";
const name = "MyName";
snipeSdk
.signUp(hash, email, phone, name)
.then((res) => {
snipe_id = res.data.value.snipe_id;
})
.catch((err) => {
console.log(err);
});
In this example, the signUp
function is called with a user hash, email, phone number, and name.
Update User
The updateUser
function is utilized to update existing user information. It allows for updating the user's email, phone number, and name.
Syntax
async snipeSdk.updateUser(snipeUserId: string, email?: string, phone?: string, name?: string)
Parameters
snipeUserId
: A string representing the user's ID. This parameter is required.email
: An optional string representing the user's new email address.phone
: An optional string representing the user's new phone number.name
: An optional string representing the user's new name.
Return Value
This function returns a Promise
that resolves to a string. The string is a message indicating the result of the update operation.
Example Usage
const snipeUserId = "user_id";
const email = "[email protected]";
const phone = "9876543210";
const name = "NewName";
snipeSdk
.updateUser(snipeUserId, email, phone, name)
.then((res) => {
console.log(`Update response: ${res}`);
})
.catch((err) => {
console.log(err);
});
In this example, the updateUser
function is called with the user ID and new details for email, phone number, and name. The function will update the user's information based on the provided details.
Notes
- It is important to have the
snipeUserId
for this function to identify which user to update. - The function checks if at least one optional parameter (email, phone, name) is provided. If none are provided, it throws an error indicating "No data given to update".
- The function also handles API key initialization and checks for a valid base URL, ensuring secure and valid requests.
- Errors, including network errors and those returned from the API, are handled and propagated for better error handling in the application.
Track Event
const eventOptions = {
eventId: "event_id",
snipeUserId: "user_id",
transactionAmount: 100, // Optional
partialPercentage: 50, // Optional
debitTokens: { token1: 10, token2: 20 }, // Optional
};
snipeSdk
.trackEvent(eventOptions)
.then((res) => {
console.log("trackEvent: ", res);
})
.catch((err) => {
console.log(err);
});
Track Event V2
const eventOptionsV2 = {
eventId: "event_id",
snipeUserId: "user_id",
userHash: "",
transactionAmount: 100, // Optional
partialPercentage: 50, // Optional
debitTokens: { token1: 10, token2: 20 }, // Optional
metadata: {"product_id":"nike_1"}, // Optional
quantity: 5 // Optional
user_metadata: {"title":"this is user_metadata"} // Optional
};
snipeSdk.trackEventV2(eventOptionsV2)
.then(res => {
console.log("trackEvent: ", res);
})
.catch(err => {
console.log(err);
});
Parameters of the trackEventV2
Function:
- eventId: A unique identifier for the specific event being tracked.
- snipeUserId: The unique ID of the user associated with the event; this is optional.
- userHash: A unique hash representing the user; this is an alternative to
snipeUserId
and is also optional. - transactionAmount: The monetary value associated with the event, if applicable; this is an optional parameter.
- partialPercentage: An optional percentage value relevant to the event, used in cases where partial metrics are involved.
- debitTokens: An object specifying different token types and their amounts, used for events involving token transactions; this is optional.
- metadata: A generic object allowing for additional, custom data related to the event; optional for providing extra context.
- quantity: The number of items or units relevant to the event; this is an optional parameter.
- user_metadata: Custom metadata specific to the user, intended to be stored as it is in the user transaction ledger; this is optional.
NOTE: snipeUserId and userHash both cannot be null, one has to be sent.
Bulk Track Event
const bulkeventOptions = [
{
snipeUserId: "6572bfa8be79a029f4ec5beb",
// Optional parameters
// transactionAmount: 100,
// partialPercentage: 50,
// debitTokens: { token1: 10, token2: 20 },
metadata: {"product_id":"nike_1"}
quantity: 5
},
];
snipeSdk
.bulkTrackEvent(bulkeventOptions)
.then((res) => {
console.log("bulkTrackEvent: ", res);
})
.catch((err) => {
console.log(err);
});
Bulk Track Event V2
const bulkeventOptionsV2 = [
{
snipeUserId: "6572bfa8be79a029f4ec5beb",
// Optional parameters
// transactionAmount: 100,
// partialPercentage: 50,
// debitTokens: { token1: 10, token2: 20 },
metadata: {"product_id":"nike_1"}
quantity: 5
},
];
snipeSdk
.bulkTrackEventV2(bulkeventOptionsV2)
.then((res) => {
console.log("bulkTrackEvent: ", res);
})
.catch((err) => {
console.log(err);
});
Get Token History
snipeSdk
.getokenHistory({ snipeUserId: "user_id" })
.then((res) => {
console.log("getokenHistory: ", res);
})
.catch((err) => {
console.log(err);
});
Get Token Details
snipeSdk
.getokenDetails({ snipeUserId: "user_id" })
.then((res) => {
console.log("getokenDetails: ", res);
})
.catch((err) => {
console.log(err);
});
Get Coin Data
snipeSdk
.getCoinData({ snipeUserId: "user_id" })
.then((res) => {
console.log("getCoinData: ", res);
})
.catch((err) => {
console.log(err);
});
Get Milestone Event Data
snipeSdk
.getMilestone({ snipeUserId: "user_id" })
.then((res) => {
console.log("getMilestone: ", res);
})
.catch((err) => {
console.log(err);
});
Get Token History with Filters
snipeSdk
.getokenHistoryWithFilter({
snipeUserId: "user_id",
queryParameters: {
page: 1,
page_size: 10,
transaction_type: "credit",
},
})
.then((res) => {
console.log("getokenHistoryWithFilter: ", res);
})
.catch((err) => {
console.log(err);
});
Note: Ensure to replace "your_api_key" with your actual API key and adjust the example parameters as per your application's context.