This directory contains some code samples to illustrate how to use the services supported in the Sinch Node.js SDK.
Before trying to run a sample, make sure you have Node.js installed: please download and install the LTS version from nodejs.org.
Once installed, check you can run node with the following command:
node --version
You should see a result such as v20.10.0
, matching the LTS you have downloaded or the version you had previously installed.
To use the Sinch Node.js SDK packages, you will need a package manager such as NPM
(already installed with Node.js) or yarn
.
To install yarn
(optional), run the following command:
npm install --global yarn
To be able to send requests to the Sinch APIs, you need to have a Sinch account. Please sign up or log in if you already have one.
With the credentials found on the Sinch dashboards, you will have to fill the file .env.template
and rename it to .env
. This file needs to be located at the same place where you will be executing the sample files.
# Credentials for APIs using OAuth2 authentication
SINCH_PROJECT_ID=project-id found at https://dashboard.sinch.com/account/access-keys
SINCH_KEY_ID=access-key-id found at https://dashboard.sinch.com/account/access-keys
SINCH_KEY_SECRET=access-key-secret found at access-key creation time
# Credentials for legacy SMS API usage
SINCH_SERVICE_PLAN_ID=service-plan-id found at https://dashboard.sinch.com/sms/api/services
SINCH_API_TOKEN=api-token found at https://dashboard.sinch.com/sms/api/services
SMS_REGION=Value to determine according to your use case (default will be 'us')
# Application credentials for Verification and Voice APIs
SINCH_APPLICATION_KEY=application-key found at https://dashboard.sinch.com/verification/apps
SINCH_APPLICATION_SECRET=application-secret found at https://dashboard.sinch.com/verification/apps
# The phone number you will use for your tests
PHONE_NUMBER=phone-number to fill with one of your numbers rented with the Numbers API
NUMBER_CALLBACK_URL=URL to receive callbacks relative a number you own
# Generally, you phone number to interact with the API
RECIPIENT_PHONE_NUMBER=phone-number to fill with the number to which you want to send a batch with the SMS API
## SMS API
BATCH_ID=batch-id to fill with one the batches created with the SMS API
GROUP_ID=group-id to fill with one of the groups created with the SMS API
INBOUND_ID=inbound-id to fill with one of the ids found when listing inbound messages with the SMS API
## Verification API
VERIFICATION_ID=verification-id to fill with the verification started with the Verification API
VERIFICATION_IDENTITY=verification-identity to fill with the identity of the user
VERIFICATION_REFERENCE=verification-reference to add when starting a verification or getting its status
VERIFICATION_CODE=verification-code received for a verification via SMS or callout
VERIFICATION_CLI=verification-cli received for a verification via flashCall
## Voice API
CALL_ID=call_id to fill with one of the callouts created with the Voice API
CONFERENCE_ID=unique identifier of the conference you want to interact with
VOICE_CALLBACK_URL=URL found in the Voice dashboard to handle webhooks
## Conversation API
CONVERSATION_APP_ID=app_id (Conversation App) to fill with one of the Conversation App created with the Conversation API or in the Dashboard
CONVERSATION_CONTACT_ID=contact_id to fill with one of the contacts created by the Contact API or the Conversation API
MESSENGER_USER_ID=identity on the MESSENGER channel (can be found on a desktop by selecting a user: the user id will be in the URL)
MESSENGER_TOKEN=static_token to define credentials for a MESSENGER channel
CONVERSATION_ID=conversation_id to fill with one of the conversations created with the Conversation API
EVENT_ID=event_id to fill with a sent event
MESSAGE_ID=message_id to fill with one of the messages sent or injected with the Conversation API
TEMPLATE_ID=template_id to fill with one of the templates created with the Templates API (v1 or v2)
WEBHOOK_ID=webhook_id to fill with one of the webhooks created with the Conversation API or the Dashboard
WEBHOOK_TARGET=target URL where the events should be sent to
## Fax API
FAX_SERVICE_ID=serviceId to fill with one the fax services created with the Fax API
FAX_ID=id from a sendFax response
FAX_CALLBACK_URL=callback url to override the one defined in the default service or specified service
FAX_EMAIL=email to associate with a phone number to use the fax-to-email functionality
## Elastic SIP Trunking API
SIP_TRUNK_ID=sipTrunkId to fill with one of the SIP trunks created with the Elastic SIP Trunking API
SIP_ENDPOINT_ID=sipEndpointId to fill with one of the SIP endpoints created with the Elastic SIP Trunking API
ACL_ID=accessControlListId to fill with one of the access control lists created with the Elastic SIP Trunking API
IP_RANGE_ID=ipRangeId to fill with one of the IP ranges added to an ACL with the Elastic SIP Trunking API
Note: If you prefer using environment variables, the sample app is also supporting them: they take precedence over the value from the .env
file.
You will find all the scripts in the package.json
file to run the samples.
npm run numbers:regions:list
yarn run numbers:regions:list
Note: You can add the option pretty
at the end of the command to display a processed output from the raw JSON response (e.g. yarn run numbers:regions:list pretty
)
Service | Sample application name and location | Required parameters |
---|---|---|
Regions | ./src/numbers/regions/list.ts | |
Available | ./src/numbers/available/list.ts | |
./src/numbers/available/checkAvailability.ts | PHONE_NUMBER |
|
./src/numbers/available/rent.ts | PHONE_NUMBER + NUMBER_CALLBACK_URL + SINCH_SERVICE_PLAN_ID + SINCH_APPLICATION_KEY |
|
./src/numbers/available/rentAny.ts | NUMBER_CALLBACK_URL + SINCH_SERVICE_PLAN_ID + SINCH_APPLICATION_KEY |
|
Active | ./src/numbers/active/list.ts | |
./src/numbers/active/get.ts | PHONE_NUMBER |
|
./src/numbers/active/update.ts | PHONE_NUMBER + SINCH_SERVICE_PLAN_ID + NUMBER_CALLBACK_URL |
|
./src/numbers/active/release.ts | PHONE_NUMBER |
|
Callbacks | ./src/numbers/callbacks/get.ts | |
./src/numbers/callbacks/update.ts |
Service | Sample application name and location | Required parameters |
---|---|---|
Services | ./src/fax/services/create.ts | PHONE_NUMBER |
./src/fax/services/get.ts | FAX_SERVICE_ID |
|
./src/fax/services/list.ts | ||
./src/fax/services/listNumbers.ts | FAX_SERVICE_ID |
|
./src/fax/services/listEmailsForNumber.ts | PHONE_NUMBER + FAX_SERVICE_ID |
|
./src/fax/services/update.ts | FAX_SERVICE_ID |
|
./src/fax/services/delete.ts | FAX_SERVICE_ID |
|
Faxes | ./src/fax/faxes/send-filePaths.ts | PHONE_NUMBER + FAX_CALLBACK_URL |
./src/fax/faxes/send-fileBase64.ts | PHONE_NUMBER + FAX_CALLBACK_URL |
|
./src/fax/faxes/send-multipleRecipients.ts | PHONE_NUMBER + FAX_CALLBACK_URL |
|
./src/fax/faxes/get.ts | FAX_ID |
|
./src/fax/faxes/list.ts | ||
./src/fax/faxes/downloadContent.ts | FAX_ID |
|
./src/fax/faxes/deleteContent.ts | FAX_ID |
|
Emails | ./src/fax/emails/add.ts | FAX_EMAIL + PHONE_NUMBER |
./src/fax/emails/list.ts | ||
./src/fax/emails/listNumbers.ts | FAX_EMAIL |
|
./src/fax/emails/update.ts | FAX_EMAIL + PHONE_NUMBER |
|
./src/fax/emails/delete.ts | FAX_EMAIL |