Skip to content

Instantly share code, notes, and snippets.

@leadbi
Created October 4, 2021 20:51
Show Gist options
  • Save leadbi/786f26c8c6cab85cafea34df5d5e491d to your computer and use it in GitHub Desktop.
Save leadbi/786f26c8c6cab85cafea34df5d5e491d to your computer and use it in GitHub Desktop.
LeadBI Import API
csv_email csv_first_name csv_last_name csv_custom1
demo@example.com John Doe custom1
tes@example.com Jane Doe custom1
hi@leadbi.com Hi LeadBI custom1
/**
* This file contains examples on how to import contacts
* to a new list and how to add / update contacts in an existing list
*/
const fs = require('fs');
const fetch = require('node-fetch');
const FormData = require('form-data');
/**
* Define constants
*/
const API_ENDPOINT = `https://app.leadbi.com`; // The api endpoint
const websiteId = 75; // The id of the target website
const credentials = { // The api keys credentials
'X-Access-Id': 'your access id',
'X-Access-Secret': 'your access secret'
}
/**
* This function parses a csv or xlsx file to json and it can also
* store the file on the server side to be used for import
* @param {string} importFilePath - the path to the source file
* @param {string} options.type - specify if the file uploaded is 'csv' or 'xlsx'
* @param {bool} options.upload_file - needs to be set to true if the resulting json needs to be stored on the server
* @param {number} options.rows_limit - the number of rouse from the parsed file to be returned back
* @returns - Returns an result object
*/
async function uploadFile(importFilePath, options) {
// Create file read stream
let fileStream = fs.createReadStream(importFilePath);
// Create file upload form
const formData = new FormData();
formData.append('file', fileStream);
formData.append('options', JSON.stringify(options));
// Select endpoint based on type
const endpoint = options.type = 'xlsx' ?
`${API_ENDPOINT}/api/v1/tools/xlsx/parse_simple` :
`${API_ENDPOINT}/api/v1/tools/csv/parse_simple`;
// Make request
const result = await fetch(endpoint, {
method: 'POST',
body: formData,
headers: {
...credentials,
}
});
// Return the parsed json
return result.json();
}
/**
* Create a new import task
* @param {number} websiteId - The id of the target website
* @param {object} payload - The payload of the task
* @param {string} payload.name - The name of the task
* @param {Array<object>} payload.contacts - An array of contacts to be imported
* @param {Array<string>} payload.tags - List of tags to be added to the list
* @param {string} payload.type - The type of import
* @param {object} payload.metadata - Additional metadata about the import
* @returns - An object with the id of the created task
*/
async function createImportTask(websiteId, payload) {
const endpoint = `${API_ENDPOINT}/api/v1/imports/${websiteId}/`;
const response = await fetch(endpoint, {
method: 'POST',
body: JSON.stringify(payload),
headers: { ...credentials, 'Content-Type': 'application/json' }
});
return await response.json();
}
(async function main() {
// Upload file
const importFileName = './docs/examples/import.csv';
const uploadResult = await uploadFile(importFileName, {
type: 'csv', // csv or xlsx
upload_file: false, // The file needs to be saved on the server after parsing
rows_limit: 10000, // rows of data to be returned
});
console.log('uploadResult', uploadResult);
const contacts = uploadResult.data.map((row) => {
return {
email: row.csv_email,
first_name: row.csv_first_name,
last_name: row.csv_last_name,
custom1: row.csv_custom1
}
});
// Add or update leads
const result = await createImportTask(websiteId, {
name: 'example leads import',
contacts: contacts,
tags: ['exampleTag'],
type: 'contacts',
metadata: {}
});
console.log('result', result)
})();
/**
* This file contains examples on how to import contacts
* to a new list and how to add / update contacts in an existing list
*/
const fs = require('fs');
const fetch = require('node-fetch');
const FormData = require('form-data');
/**
* Define constants
*/
const API_ENDPOINT = `https://app.leadbi.com`; // The api endpoint
const websiteId = 75; // The id of the target website
const existingListId = 8277; // An existing list id
const credentials = { // The api keys credentials
'X-Access-Id': 'your access id',
'X-Access-Secret': 'your access secret'
}
/**
* This function parses a csv or xlsx file to json and it can also
* store the file on the server side to be used for import
* @param {string} importFilePath - the path to the source file
* @param {string} options.type - specify if the file uploaded is 'csv' or 'xlsx'
* @param {bool} options.upload_file - needs to be set to true if the resulting json needs to be stored on the server
* @param {number} options.rows_limit - the number of rouse from the parsed file to be returned back
* @returns - Returns an result object
*/
async function uploadFile(importFilePath, options) {
// Create file read stream
let fileStream = fs.createReadStream(importFilePath);
// Create file upload form
const formData = new FormData();
formData.append('file', fileStream);
formData.append('options', JSON.stringify(options));
// Select endpoint based on type
const endpoint = options.type = 'xlsx' ?
`${API_ENDPOINT}/api/v1/tools/xlsx/parse_simple` :
`${API_ENDPOINT}/api/v1/tools/csv/parse_simple`;
// Make request
const result = await fetch(endpoint, {
method: 'POST',
body: formData,
headers: {
...credentials,
}
});
// Return the parsed json
return result.json();
}
/**
* Create a new import task
* @param {number} websiteId - The id of the target website
* @param {object} payload - The payload of the task
* @param {string} payload.name - The name of the task
* @param {object} payload.contacts - Metadata regarding the contacts uploaded
* @param {Array<string>} payload.contacts.columns - Array of columns found in csv
* @param {object} payload.contacts.fields - Mapping between csv fields and leadbi fields
* @param {object} payload.contacts.customFields - Mapping between csv fields and custom fields
* @param {number} payload.contacts.count - The number of records imported
* @param {object} payload.contacts.file - The file object returned from the server upload
* @param {Array<string>} payload.tags - List of tags to be added to the list
* @param {string} payload.type - The type of import
* @param {object} payload.metadata - Additional metadata about the import
* @returns - An object with the id of the created task
*/
async function createImportTask(websiteId, payload) {
const endpoint = `${API_ENDPOINT}/api/v1/imports/${websiteId}/`;
const response = await fetch(endpoint, {
method: 'POST',
body: JSON.stringify(payload),
headers: { ...credentials, 'Content-Type': 'application/json' }
});
return await response.json();
}
(async function main() {
// Upload file
const importFileName = './docs/examples/import.csv';
const uploadResult = await uploadFile(importFileName, {
type: 'csv', // csv or xlsx
upload_file: true, // The file needs to be saved on the server after parsing
rows_limit: 100, // rows of sample data to be returned
});
console.log('uploadResult', uploadResult);
// Create a new list
const result = await createImportTask(websiteId, {
name: 'example import', // also new list name
contacts: {
columns: uploadResult.columns,
fields: { // fields mapping
csv_email: 'email',
csv_first_name: 'first_name',
csv_last_name: 'last_name',
},
customFields: {
csv_custom1: 'custom1'
},
count: uploadResult.count,
file: uploadResult.file
},
tags: ['exampleTag'],
type: 'list',
metadata: {}
});
console.log('result', result)
// Update existing list
const resultUpdate = await createImportTask(websiteId, {
name: 'example update import',
contacts: {
columns: uploadResult.columns,
fields: { // fields mapping
csv_email: 'email',
csv_first_name: 'first_name',
csv_last_name: 'last_name',
},
customFields: {
csv_custom1: 'custom1'
},
count: uploadResult.count,
file: uploadResult.file
},
tags: ['exampleTag'],
type: 'list',
metadata: {
update_list: true, // specify that this is an update operation
list_id: existingListId // specify the id of the list to be updated
}
});
console.log('resultUpdate', resultUpdate)
})().catch(err => console.error(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment