# Sinch Node.js SDK for Fax API

The Sinch Node.js SDK allows you to quickly interact with the  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  are structured similarly to those that are sent and received using the .

The fastest way to get started with the SDK is to check out our [getting-started](/docs/fax/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](/docs/fax/api-reference/fax).

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.

SDK
start.js
```javascript 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);
  }
}
```

REST API
```javascript
const ACCESS_KEY = "YOUR_access_key";
const ACCESS_SECRET = "YOUR_access_secret";
const PROJECT_ID = "YOUR_project_id";
const TO_NUMBER = "YOUR_to_number";
const CONTENT_URL = "YOUR_content_url";
const CALLBACK_URL = "YOUR_callback_url";

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);

const faxBody = {
to:  TO_NUMBER,
contentUrl: CONTENT_URL,
callbackUrl: CALLBACK_URL
};

apiClient
  .post('/faxes/', faxBody)
  .then((response) => console.log(response));
```

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

- [Client initialization](#client)
- [Fax domain access](#fax-domain)
- [Endpoint usage](#endpoint-categories)
- [Field population](#request-and-query-parameters)


## 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](https://dashboard.sinch.com/dashboard).

```javascript
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**

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

**`app.js` File**

```javascript
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()]`.

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:

- [`fax.faxes`![external](/assets/external-blue.5a59beb5e5a442f78b530509ddf6b5d0fa705285ff8181e59adf96b5c2c73e25.33310b29.svg)](https://developers.sinch.com/sdk/sinch-sdk-node/1.8.0/classes/FaxesApi.html)
- [`fax.coverPages`![external](/assets/external-blue.5a59beb5e5a442f78b530509ddf6b5d0fa705285ff8181e59adf96b5c2c73e25.33310b29.svg)](https://developers.sinch.com/sdk/sinch-sdk-node/1.8.0/classes/CoverPagesApi.html)
- [`fax.faxToEmail`![external](/assets/external-blue.5a59beb5e5a442f78b530509ddf6b5d0fa705285ff8181e59adf96b5c2c73e25.33310b29.svg)](https://developers.sinch.com/sdk/sinch-sdk-node/1.8.0/classes/FaxToEmailApi.html)
- [`fax.services`![external](/assets/external-blue.5a59beb5e5a442f78b530509ddf6b5d0fa705285ff8181e59adf96b5c2c73e25.33310b29.svg)](https://developers.sinch.com/sdk/sinch-sdk-node/1.8.0/classes/ServicesApi.html)


For example:

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

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:

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

```JSON

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.

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