# Handle an incoming fax with Node.js Now that you know how to [send yourself a fax a using the Fax API](/docs/fax/getting-started/node/send-fax), learn how to handle incoming faxes. In this guide you will learn: 1. How to [set up your your Node.js app](#set-up-your-nodejs-application) 2. How to [start your server](#start-your-server) 3. About [callbacks](#understanding-callbacks) 4. How to [receive an incoming fax](#send-your-sinch-number-a-fax) ## What you need to know before you start Before you can get started, you need the following already set up: * Set all Fax API [configuration settings](/docs/fax/getting-started). * [NPM](https://www.npmjs.com/) and a familiarity with how to install packages. * [Node.js](https://nodejs.org/en/) and a familiarity with how to create a new app. * A Sinch phone number configured for the Fax API. ## Set up your Node.js application First we'll create a Node project using npm. This creates a package.json and the core dependencies necessary to start coding. To create the project, do the following steps: 1. Create a folder called `fax-receive` 2. Navigate into the folder you created and run the following command. ```shell npm init ``` This command adds the node_modules folder and 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. ### Install your dependencies We will be using Express to create a lightweight webserver that will listen for requests from the Sinch servers to handle incoming faxes. Because the webserver is running on your local machine, you will also need a method for making the server accessible over the internet. In this guide we'll be using [ngrok](https://www.ngrok.com) to do that, but if you have another way you'd prefer to do it, feel free to use that. Additionally, we'll be using the node-fetch package to make HTTP requests. Use the following command to install the Express, ngrok, and node-fetch packages: ```shell npm install express ngrok node-fetch ``` ### Create your file In your project folder, create a new file named **index.js** in the project and paste the provided "index.js" code into the file. index.js const fs = require('fs'); const express = require('express'); const app = express(); const ngrok = require('ngrok'); const PORT = 5000; app.use(express.json({limit: '25mb'})); app.use(express.urlencoded({limit: '25mb', extended: true})); const ACCESS_KEY = "YOUR_access_key"; const ACCESS_SECRET = "YOUR_access_secret"; const PROJECT_ID = "YOUR_project_id"; const SERVICE_ID = "YOUR_service_id"; let config = { baseURL: "https://fax.api.sinch.com/v3/projects/" + PROJECT_ID, auth: { username: ACCESS_KEY, password: ACCESS_SECRET } }; const apiClient = require('axios').create(config); async function updateUrl(url) { const urlUpdate = { incomingWebhookUrl: url }; apiClient .patch('/services/' + SERVICE_ID, urlUpdate) .then((response) => console.log(response)); } app.post('/', async (req, res) => { var requestBody = req.body; console.log(requestBody); if (requestBody.event == "INCOMING_FAX") { const data = new Uint8Array(Buffer.from(JSON.parse(JSON.stringify(requestBody.file)), 'base64')); fs.writeFile(requestBody.fax.id + ".pdf", data, (err) => { if (err) throw err; console.log("The file has been saved."); }); } res.sendStatus(200); }); app.listen(PORT, async () => { const url = await ngrok.connect(PORT); console.log(`Node.js local server is publicly-accessible at ${url}/incoming-call`); await updateUrl(url); console.log("Your callbackURL has been updated on your dashboard.") console.log(`Listening at http://localhost:${PORT}`); }); This code starts your webserver, creates an ngrok tunnel, updates your incoming webhook URL, and also contains the logic to download fax content from the Sinch servers. We'll come back to what those things are in a moment. Before you save your file and start your server, there's a couple of values you to need to update. ### Update your parameter values Update the values of the following parameters: | Parameter | Your value | | --- | --- | | `YOUR_access_key` | The key found on your Sinch [dashboard](https://dashboard.sinch.com/account/access-keys). | | `YOUR_access_secret` | The secret found on your Sinch [dashboard](https://dashboard.sinch.com/account/access-keys). **Note:** Access secrets are only available during initial key creation. | | `YOUR_project_id` | The project ID found on your Sinch [dashboard](https://dashboard.sinch.com/account/access-keys). | | `YOUR_service_id` | The service ID found on your Sinch [dashboard](https://dashboard.sinch.com/fax/services). | ## Start your server At this point, you can start your server with the following command: ```shell node index.js ``` You should notice a few things. - First, your console should show that the server has started on port 5000 of your localhost. - Second, the ngrok tunnel has started and you should see the URL displayed in the console. - Additionally, you should see a message in your console that the incoming webhook URL for your fax service was updated automatically. There is code in the sample that makes a [request to the Fax API](/docs/fax/api-reference/fax/services/updateservice) to update the incoming webhook URL. Tip: You can also update your incoming webhook URL in the dashboard yourself. Navigate to your fax services on your [dashboard](https://dashboard.sinch.com/fax/services). Next to your service click the **Edit** button. You'll see a field labeled "Incoming webhook URL." Enter your URL into that field and click **Save**. Now your server is listening and your incoming webhook URL is configured, so you're almost ready to test everything and send a fax that you can receive. But before we do, let's take a closer look at webhooks. If you already know about webhooks, skip right to [sending yourself a fax](#call-your-sinch-phone-number). ### Understanding webhooks Webhooks (also known as "callbacks" or "notifications") are the method that the Fax API uses to inform you when your Sinch number is sent a fax. Basically, a [notification](/docs/fax/api-reference/fax/notifications) is a request that the Sinch servers send to your server whenever something happens (otherwise known as an "event"), such as when you send or receive a fax. There are two different kinds of events, the Fax Completed and the Incoming Fax event, but the one we're concerned about is the Incoming Fax event. An *Incoming Fax* event happens whenever someone sends a fax to one of your Sinch numbers. All of the notification events the Fax API sends expect a 200 OK response in return. And that's it! Now we can test. ## Send your Sinch number a fax Using the [app we created in the previous guide](/docs/fax/getting-started/node/send-fax), send a fax to your Sinch number. The fax should be picked up by the Sinch servers and a PDF of your fax will be downloaded to your machine. Now you know how to handle an incoming fax. ## Next steps Learn more about the Fax API: - [API specification](/docs/fax/api-reference/fax)