# Look up numbers in bulk with Node.js The Number Lookup API lets you look up a phone number to verify it's in service and to check information about the number such as the carrier. In this tutorial you will learn how to look up numbers in bulk using Node.js. In this guide you will learn how to: Learn how to: 1. [Set up](#set-up-your-nodejs-application) your Node.js application 2. [Bulk look up](#bulk-look-up-your-phone-numbers) your phone numbers ## What you need to know before you start Before you can get started, you need the following already set up: * Set all Number Lookup API [configuration settings](/docs/number-lookup-api-v2/getting-started). * [Node.js](https://nodejs.org/en/) and a familiarity with how to create a new app. ## Set up your Node.js application First we'll create a Node project using npm. To create the project, do the following steps: 1. Create a folder called `number-lookup`. 2. Navigate into the folder you created and run the following command. ```shell npm init ``` This command creates the package.json file. You will be prompted to provide values for the fields. For this tutorial, you can simply accept the default values and press enter at each stage. 1. Add the fetch and event-stream packages with npm to generate the necessary dependencies. ```shell npm install 'cross-fetch' npm install 'event-stream' ``` ## Create your file Create a new file named **index.js** in the project and paste the provided "index.js" code into the file. Note: This tutorial uses basic authentication for testing purposes. We recommend using OAuth for authentication in a production environment. You can follow the steps in this guide, but use the code samples from [here](/docs/number-lookup-api-v2/api-reference/authentication/oauth) to use OAuth authentication instead. index.js // Use this code to look up numbers in bulk. const fetch = require('cross-fetch'); const fs = require('fs'); const es = require('event-stream'); // Find your application key and secret at dashboard.sinch.com/number-lookup/apps const ACCESS_KEY = "YOUR_access_key"; const ACCESS_SECRET = "YOUR_access_secret"; const PROJECT_ID = "YOUR_project_id" const FILE_PATH = "phone_numbers.txt"; const basicAuthentication = APPLICATION_KEY + ":" + APPLICATION_SECRET; async function run(){ async function bulkLookup(phoneNumArr){ console.log(phoneNumArr); for (const number of phoneNumArr){ await lookupNumber(number); } } async function readFile(file){ var phoneNumbersArr = []; return new Promise((resolve, reject) => { const stream = fs.createReadStream(file) .pipe(es.split()) .pipe(es.mapSync(function(line){ stream.pause(); phoneNumbersArr.push(line); stream.resume(); }) .on('error', function(err) { console.log('Error: ', err); reject(); }) .on('end', function() { resolve(phoneNumbersArr); }));})} function writeToFile(text){ fs.appendFile('results.txt', text + '\n', function(err) { if (err) { console.log(err); return; } else { return; } }); } async function lookupNumber(num) { const lookup = { number: num }; await fetch("https://lookup.api.sinch.com/v2/projects" + PROJECT_ID + "/lookups", { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Basic ' + Buffer.from(basicAuthentication).toString('base64') }, body: JSON.stringify(lookup) }).then(res => res.json()).then(json => writeToFile(num + '\n' + JSON.stringify(json))); } await bulkLookup(await readFile(FILE_PATH)); } run(); This code contains the logic to read in a list of phone numbers in a text file, look up those numbers, and then write the results of the number lookup calls to a new text file named `results.txt` in the project folder. To make this tutorial work, you should have a text file of phone numbers formatted to E.164 standard, with each number on a new line. For example, the text file should look like this: ```text +12345678900 +13334445656 +15556789999 ``` ## Fill in your parameters Assign your values to the following parameters: | Parameter | Your value | | --- | --- | | `ACCESS_KEY` | The key found on your Sinch [Build dashboard](https://dashboard.sinch.com/settings/access-keys). | | `ACCESS_SECRET` | The secret found on your Sinch [Build dashboard](https://dashboard.sinch.com/settings/access-keys). | | `PROJECT_ID` | Your project ID found on your Sinch [Build dashboard](https://dashboard.sinch.com/settings/access-keys). | | `FILE_PATH` | This is the file path to the text file containing the phone numbers you want to look up. | Save the file. ## Bulk look up your phone numbers Now you can execute the code and look up your phone numbers. Run the following command: ```shell node index.js ``` The application should create a text file named `results.txt` in your project folder, containing the information responses for each of the phone numbers in your original list of numbers. Troubleshooting tip If after running your app you receive a 5000 error response, you may have forgotten to save your file after adding your authentication values. This is an easy mistake to make! Try saving the file and running the app again. ## Additional resources - [API specification](/docs/number-lookup-api/api-reference/)