Skip to content

Sinch Node.js SDK for Fax API

The Sinch Node.js SDK allows you to quickly interact with the Fax API from inside your Node.js applications. When using the Node.js SDK, the code representing requests and queries sent to and responses received from the Fax API are structured similarly to those that are sent and received using the Fax API.

The fastest way to get started with the SDK is to check out our getting-started guides. There you'll find all the instructions necessary to download, install, set up, and start using the SDK.

Syntax

Note:

This guide describes the syntactical structure of the Node.js SDK for the Fax API, including any differences that may exist between the API itself and the SDK. For a full reference on Fax API calls and responses, see the Fax API Reference.

The code sample below is an example of how to use the Node.js SDK to send a fax. We've also provided an example that accomplishes the same task using the REST API.

start.js
/**
 * Class to send a demo fax using the Sinch Node.js SDK.
 */
export class FaxSample {
  /**
   * @param { import('@sinch/sdk-core').FaxService } faxService - the FaxService instance from the Sinch SDK containing the API methods.
   */
  constructor(faxService) {
    this.faxService = faxService;
  }

  async start() {
    const recipient = 'YOUR_FAX_NUMBER';
    const contentUrl = 'https://developers.sinch.com/fax/fax.pdf';
    const callbackUrl = 'FAX_CALLBACK_URL';

    const response = await this.faxService.faxes.send({
      sendFaxRequestBody: {
        to: recipient,
        contentUrl,
        ...(callbackUrl !== 'FAX_CALLBACK_URL' && { callbackUrl }),
      },
    });

    const fax = response[0];
    console.log('Fax ID:', fax.id);
    console.log('Status:', fax.status);
    console.log('Response:', response);
  }
}

This example highlights the following required to successfully make a Fax API call using the Sinch Node.js SDK:

Client

When using the Sinch Node.js SDK, you initialize communication with the Sinch backend by initializing the Node.js SDK's main client class. This client allows you to access the functionality of the Sinch Node.js SDK.

Initialization

To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard.

const {SinchClient} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    projectId: "YOUR_project_id",
    keyId: "YOUR_access_key",
    keySecret: "YOUR_access_secret"
});
Note

For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials, as in the following example:

.env File

PROJECTID="YOUR_project_id"
ACCESSKEY="YOUR_access_key"
ACCESSSECRET="YOUR_access_secret" 

app.js File

const {SinchClient} = require('@sinch/sdk-core');

const sinchClient = new SinchClient({
    projectId: process.env.PROJECTID,
    keyId: process.env.ACCESSKEY,
    keySecret: process.env.ACCESSSECRET
});

Fax domain

The Sinch Node.js SDK organizes different functionalities in the Sinch product suite into domains. These domains are accessible through the client. For example, sinch.fax.[endpoint_category].[method()].

Endpoint categories

In the Sinch Node.js SDK, Fax API endpoints are accessible through the client. The naming convention of the endpoint's representation in the SDK matches the API:

For example:

const response = await fax.faxes.send({
      sendFaxRequestBody: {
        to: recipient,
        contentUrl,
        ...(callbackUrl !== 'FAX_CALLBACK_URL' && { callbackUrl }),
      },
});

Request and query parameters

Requests and queries made using the Node.js SDK are similar to those made using the Fax API. Path parameters, request body parameters, and query parameters that are used in the API are all passed as arguments to the corresponding Node.js method.

For example, consider this example in which the get() method of the fax.faxes class is invoked:

const response = await sinch.fax.faxes.get({ id: faxId });

url = "https://fax.api.sinch.com/v3/projects/${projectId}/faxes/${faxId}"

When using the Fax API, faxId would be included as a path parameter in the request. With the Node.js SDK, the faxId parameter is used in an object passed as an argument in the get() method.

Responses

Response fields match the API responses. They are delivered as Javascript objects.