yournotify-node-sdk
Version:
NPM SDK for Yournotify API. Send email and sms marketing campaigns with ease.
109 lines (89 loc) • 2.9 kB
JavaScript
class Yournotify {
constructor(apiKey) {
this.apiKey = apiKey;
this.apiUrl = "https://api.yournotify.com/";
}
async request(endpoint, method = 'GET', data = {}) {
const url = `${this.apiUrl}${endpoint}`;
const headers = {
"Authorization": `Bearer ${this.apiKey}`,
"Content-Type": "application/json"
};
const options = { method, headers };
if (['POST', 'PUT'].includes(method)) {
options.body = JSON.stringify(data);
}
const response = await fetch(url, options);
return response.json();
}
async sendEmail(name, subject, html, text, status, from, to) {
const data = {
name,
subject,
html,
text,
from,
status,
channel: "email",
lists: to
};
return this.request("campaigns/email", 'POST', data);
}
async sendSMS(name, from, text, status, to) {
const data = {
name,
from,
text,
status,
channel: "sms",
lists: to
};
return this.request("campaigns/sms", 'POST', data);
}
async addContact(email, telephone, list, name, attribs) {
const data = { email, telephone, lists: [list], name, attribs };
return this.request("contacts", 'POST', data);
}
async getContact(id) {
return this.request(`contacts/${id}`, 'GET');
}
async getContacts() {
return this.request("contacts", 'GET');
}
async deleteContact(id) {
return this.request(`contacts/${id}`, 'DELETE');
}
async addList(title, type, optin) {
const data = { title, type, optin };
return this.request("lists", 'POST', data);
}
async getList(id) {
return this.request(`lists/${id}`, 'GET');
}
async getLists() {
return this.request("lists", 'GET');
}
async deleteList(id) {
return this.request(`lists/${id}`, 'DELETE');
}
async deleteCampaign(id) {
return this.request(`campaigns/${id}`, 'DELETE');
}
async getCampaignStats(ids = [], channel = 'email') {
const idParam = Array.isArray(ids) ? ids.join(',') : ids;
return this.request(`campaigns/analytics/stats?id=${idParam}&channel=${channel}`, 'GET');
}
async getCampaignReports(ids = [], channel = 'email') {
const idParam = Array.isArray(ids) ? ids.join(',') : ids;
return this.request(`campaigns/analytics/reports?id=${idParam}&channel=${channel}`, 'GET');
}
async getProfile() {
return this.request("account/profile", 'GET');
}
}
// CJS Export
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Yournotify;
}
// ESM Export
export default Yournotify;