{"id":5229,"date":"2022-12-27T16:06:54","date_gmt":"2022-12-27T15:06:54","guid":{"rendered":"https:\/\/www.smsapi.com\/blog\/?p=5229"},"modified":"2024-01-29T14:49:03","modified_gmt":"2024-01-29T13:49:03","slug":"javascript-sms-api-library-code-snippets-error-codes","status":"publish","type":"post","link":"https:\/\/www.smsapi.com\/blog\/javascript-sms-api-library-code-snippets-error-codes\/","title":{"rendered":"JavaScript SMS API: library, code snippets, error codes"},"content":{"rendered":"\n<p><strong>Enter the SMSAPI JavaScript library! Sending SMS with JS is simpler than you think. In this programmer\u2019s guide, you\u2019ll find both sample code fragments and a discussion of how to do things and how to tackle errors you may encounter.<\/strong><\/p>\n\n\n\n<!--more-->\n\n\n\n<p>I dealt with other SMS APIs libraries like <a href=\"https:\/\/www.smsapi.com\/blog\/java-sms-guide-2023\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a>,&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/blog\/send-sms-with-php-library\/\" target=\"_blank\">PHP<\/a>, <a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/blog\/send-sms-python-api\/\" target=\"_blank\">Python<\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/blog\/send-sms-c-library-sms-api\/\" target=\"_blank\">C#<\/a>, and I\u2019ve even shown what to do with the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/blog\/receiving-sms-online-developer-guide\/\" target=\"_blank\">incoming message<\/a>. But this time, I\u2019ll concentrate on the <strong>outbound messages sent via JavaScript Library<\/strong>. Lock and load! First off \u2013 a public service announcement: in January 2021, we released version 2.0.0 of the JavaScript library.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button is-style-secondary\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/www.smsapi.com\/en\/sms-api\" target=\"_blank\" rel=\"noreferrer noopener\">SMS API Libraries<\/a><\/div>\n<\/div>\n\n\n\n<p>What\u2019s changed? Quite a lot, as it introduces TypeScript support. It is a lesser-known \u201csuperstructure\u201d for JavaScript (professionally, its superset), made by Microsoft. It means that <strong>any JavaScript code is also correct in TypeScript<\/strong>. The essential difference is that static typing has been added to the fully dynamic JS. Additionally, TypeScript is the first to support the important async\/await mechanism. But all in good time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Before you start sending SMS messages<\/h2>\n\n\n\n<p>You need to take a few steps of preparation before you deliver your first SMS. These will involve&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/en\/signup\" target=\"_blank\">creating and configuring an account<\/a>&nbsp;on our website or <a rel=\"noreferrer noopener\" href=\"https:\/\/ssl.smsapi.com\/\" target=\"_blank\">logging<\/a> into yours. Either way, get <strong>access to the Customer Portal<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-smsapi-blog-2021-infobox infobox infobox--attention post-content--full-width\"><div class=\"infobox__icon\"><img decoding=\"async\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiB2aWV3Qm94PSIwIDAgMTAgMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgY2xpcC1ydWxlPSJldmVub2RkIiB4bWw6c3BhY2U9InByZXNlcnZlIiB2ZXJzaW9uPSIxLjEiPgogIDxwYXRoIGlkPSJzdmdfMSIgc3R5bGU9ImZpbGw6IHJnYigxNywgMTkyLCAxMjgpOyBmaWxsLXJ1bGU6IG5vbnplcm87IiBkPSJtMi45MTk2NCwwbDQsMGwwLDRsLTEsN2wtMiwwbC0xLC03bDAsLTR6Ii8+CiAgPHBhdGggaWQ9InN2Z18yIiBzdHlsZT0iZmlsbDogcmdiKDE3LCAxOTIsIDEyOCk7IGZpbGwtcnVsZTogbm9uemVybzsiIGQ9Im03LjAwODM3LDE0YzAsMS4xMDUgLTAuODk1LDIgLTIsMmMtMS4xMDUsMCAtMiwtMC44OTUgLTIsLTJjMCwtMS4xMDUgMC44OTUsLTIgMiwtMmMxLjEwNSwwIDIsMC44OTUgMiwyeiIvPgo8L3N2Zz4K\"\/><\/div><div class=\"infobox__title\"><h4>Free tests<\/h4><\/div><div class=\"infobox__content\">\n<p>Test our open SMS API and JavaScript library free of charge. Create a trial business account.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button is-style-primary\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/www.smsapi.com\/en\/signup\" target=\"_blank\" rel=\"noreferrer noopener\">Test SMSAPI<\/a><\/div>\n<\/div>\n<\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Selected features of the SMSAPI web portal<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.smsapi.com\/en\/bulk-sms\" target=\"_blank\" rel=\"noreferrer noopener\">bulk SMS sending<\/a>;<\/li>\n\n\n\n<li>text message scheduling;<\/li>\n\n\n\n<li><a href=\"https:\/\/www.smsapi.com\/blog\/hlr-lookup-check-number\/\" target=\"_blank\" rel=\"noreferrer noopener\">HLR lookup<\/a>;<\/li>\n\n\n\n<li>MMS messages;<\/li>\n\n\n\n<li>2Way SMS;<\/li>\n\n\n\n<li>authentication credentials (2FA SMS);<\/li>\n\n\n\n<li>OAuth2 tokens for connecting to the API;<\/li>\n\n\n\n<li>SMS sender names (SMS branding);<\/li>\n\n\n\n<li>URL shortener;<\/li>\n\n\n\n<li>contact lists;<\/li>\n\n\n\n<li>recipient groups;<\/li>\n\n\n\n<li>SMS opt-out system;<\/li>\n\n\n\n<li>invoicing information;<\/li>\n\n\n\n<li>payments for <a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/en\/sms-campaigns\" target=\"_blank\">SMS campaigns<\/a>.<\/li>\n<\/ul>\n\n\n\n<p>As you can see, the <a href=\"https:\/\/www.smsapi.com\/blog\/sms-gateway-starting-guide\/\" target=\"_blank\" rel=\"noreferrer noopener\">SMSAPI gateway<\/a> and its Customer Portal itself gives you decent possibilities. Find the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/blog\/do-it-yourself\/\" target=\"_blank\">complete guide to setting up the SMSAPI account<\/a>. Other texts and video guides on the blog showcase individual features of the panel \u2013 check out the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/blog\/tutorial\/\" target=\"_blank\">Tutorials tab<\/a>. Now let\u2019s focus on the code.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-smsapi-blog wp-block-embed-smsapi-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"6eTPgGDPK9\"><a href=\"https:\/\/www.smsapi.com\/blog\/do-it-yourself-customer-portal-walkthrough\/\">Do it yourself #01 &#8211; Customer Portal Walkthrough (video tutorial)<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Do it yourself #01 &#8211; Customer Portal Walkthrough (video tutorial)&#8221; &#8212; SMSAPI Blog\" src=\"https:\/\/www.smsapi.com\/blog\/do-it-yourself-customer-portal-walkthrough\/embed\/#?secret=y4HVjgYBp6#?secret=6eTPgGDPK9\" data-secret=\"6eTPgGDPK9\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>We have divided each Library into modules responsible for individual functions. You can find all of it on the&nbsp;SMSAPI GitHub. Analysing individual files is, of course, the surest way to learn how to handle them. But, if you\u2019d prefer to familiarise yourself while using it a little faster and with more fun \u2013 read on!<\/p>\n\n\n\n<div class=\"wp-block-smsapi-blog-2021-infobox infobox infobox--info post-content--full-width\"><div class=\"infobox__icon\"><img decoding=\"async\" src=\"data:image\/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxzdmcKICAgd2lkdGg9IjEwMCUiCiAgIGhlaWdodD0iMTAwJSIKICAgdmlld0JveD0iMCAwIDEyIDE2IgogICBjbGlwLXJ1bGU9ImV2ZW5vZGQiCiAgIHhtbDpzcGFjZT0icHJlc2VydmUiCiAgIHZlcnNpb249IjEuMSIKICAgaWQ9InN2ZzYzIgogICB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciCiAgIHhtbG5zOnN2Zz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogIDxwYXRoCiAgICBzdHlsZT0iZmlsbDogcmdiKDE3LCAxOTIsIDEyOCk7IGZpbGwtcnVsZTogbm9uemVybzsiCiAgIGQ9Im03LjUxNDA1LDExbC0zLDBjMCwtMyAxLjYsLTQgMi43LC00LjZjMC40LC0wLjIgMC43LC0wLjQgMC45LC0wLjZjMC41LC0wLjUgMC4zLC0xLjIgMC4yLC0xLjRjLTAuMywtMC43IC0xLC0xLjQgLTIuMywtMS40Yy0yLjEsMCAtMi41LDEuOSAtMi41LDIuM2wtMywtMC40YzAuMiwtMS43IDEuNywtNC45IDUuNSwtNC45YzIuMywwIDQuMywxLjMgNS4xLDMuMmMwLjcsMS43IDAuNCwzLjUgLTAuOCw0LjdjLTAuNSwwLjUgLTEuMSwwLjggLTEuNiwxLjFjLTAuOSwwLjUgLTEuMiwxIC0xLjIsMnoiCiAgIGlkPSJwYXRoNTkiIC8+CiAgPHBhdGgKICAgIHN0eWxlPSJmaWxsOiByZ2IoMTcsIDE5MiwgMTI4KTsgZmlsbC1ydWxlOiBub256ZXJvOyIKICAgZD0ibTgsMTRjMCwxLjEwNSAtMC44OTUsMiAtMiwyYy0xLjEwNSwwIC0yLC0wLjg5NSAtMiwtMmMwLC0xLjEwNSAwLjg5NSwtMiAyLC0yYzEuMTA1LDAgMiwwLjg5NSAyLDJ6IgogICBpZD0icGF0aDYxIiAvPgo8L3N2Zz4K\"\/><\/div><div class=\"infobox__title\"><h4>Business uses of SMS texts<\/h4><\/div><div class=\"infobox__content\">\n<ul class=\"wp-block-list\">\n<li>Sending and receiving SMS from clients<\/li>\n\n\n\n<li><a href=\"https:\/\/www.smsapi.com\/en\/sms-notifications\" target=\"_blank\" rel=\"noreferrer noopener\">SMS notifications<\/a> and customer service<\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/en\/sms-marketing\" target=\"_blank\">SMS marketing<\/a> and sales-oriented messaging<\/li>\n\n\n\n<li>Two-factor authentication (SMS 2FA)<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Sending SMS messages through SMS API JavaScript<\/h2>\n\n\n\n<p>The foundation of automatic SMS handling are the HTTP methods. An HTTP client is an application\u2019s core for communicating with the SMS API, which formulates these server queries through HTTP requests with all the necessary parameters. An example of such a solution is the popular Axios, written in JavaScript. It\u2019s a solution our Library uses for making API requests. This way, you don\u2019t have to provide your client.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Preparing to work with the JavaScript SMS API<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Installation<\/h4>\n\n\n\n<p>Use node.js package manager of your choice to install the library.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>\/\/npm\nnpm install smsapi\n\n\/\/yarn\nyarn add smsapi\n\n\/\/pnpm\npnpm install smsapi<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Initialisation<\/h4>\n\n\n\n<p>You\u2019ll need to initialise the Library\u2019s functions when working with the&nbsp;code. Nothing could be simpler! All you need to do is create a new instance of SMSAPI &#8211; the only parameter you need here is the&nbsp;<strong>authorisation token<\/strong>&nbsp;(API key) generated in the Customer Portal.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-smsapi-blog wp-block-embed-smsapi-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"CxyCPMS1HQ\"><a href=\"https:\/\/www.smsapi.com\/blog\/api-smsapi-essentials-developer-guide\/\">Send SMS using API \u2013 Developer Guide #01<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Send SMS using API \u2013 Developer Guide #01&#8221; &#8212; SMSAPI Blog\" src=\"https:\/\/www.smsapi.com\/blog\/api-smsapi-essentials-developer-guide\/embed\/#?secret=AQwUWy3Jhk#?secret=CxyCPMS1HQ\" data-secret=\"CxyCPMS1HQ\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Initialisation \u2013 sample code:<\/h4>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>import {SMSAPI} from 'smsapi';\nconst apiToken = '%SMSAPI_ACCESS_TOKEN%';\nconst smsapi = new SMSAPI(apiToken);<\/code><\/pre>\n\n\n\n<p>And that\u2019s all! Axios does all the client configuration.&nbsp;A piece of advice: do not store your API key directly in your code &#8211; use non-public environment variables instead.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">JavaScript: sending a single SMS<\/h2>\n\n\n\n<p class=\"language-javascript\">What\u2019s the simplest way to send text messages and SMS notifications from a web app or JS application? Use the module with the fairly obvious name&nbsp;<code>sms<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code sample:<\/h3>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>import {SMSAPI, MessageResponse} from 'smsapi'; \/\/ MessageResponse not used\n\nconst smsapi = new SMSAPI('&#91;token]');\n\nconst receiver = '+44111222333';\n\nconst message = 'Hello world!';\nsmsapi.sms.sendSms(receiver, message);<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">The&nbsp;<code>sendSms<\/code>&nbsp;method is asynchronous, so it returns the&nbsp;<code>Promise<\/code> object known from JavaScript. This, in turn, is an envelope for the&nbsp;<a rel=\"noreferrer noopener nofollow\" href=\"https:\/\/github.com\/smsapi\/smsapi-javascript-client\/blob\/master\/src\/types\/MessageResponse.ts\" target=\"_blank\"><code>MessageResponse<\/code><\/a> object. You need to wait for a response to be able to use the returned data. Now comes the async\/await mechanism. Once sending is complete, the <strong>function below will display a status<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>async function SendAndDisplayStatus(receiver, message) {\nconst response = await smsapi.sms.sendSms(receiver, message);\nconst response: MessageResponse = await smsapi.sms.sendSms(receiver, message); \/\/ Or like this, if you use MessageResponse\n\nconsole.log(response.list&#91;0].status);\n}<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">The individual elements of the returned the <code>response<\/code>&nbsp;object&nbsp;are similar to the JSON response described in the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/docs\/#2-single-sms\" target=\"_blank\">API Documentation<\/a>. You can log response for debugging purposes. It is possible to&nbsp;<strong>send SMS to multiple phone numbers<\/strong> at once, which leads us to the element called&nbsp;<code>list<\/code>. Its fields include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>status<\/strong>&nbsp;\u2013 one of the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/docs#17-delivery-statuses\" target=\"_blank\">message delivery statuses<\/a>;<\/li>\n\n\n\n<li><strong>id<\/strong>&nbsp;\u2013 a unique identifier in the SMSAPI system.<\/li>\n\n\n\n<li><strong>points<\/strong>&nbsp;\u2013 sending cost, or the number of points deducted from the SMSAPI account;<\/li>\n\n\n\n<li><strong>number<\/strong>&nbsp;\u2013 recipient phone number;<\/li>\n\n\n\n<li><strong>dateSent<\/strong>&nbsp;\u2013 send date.<\/li>\n<\/ul>\n\n\n\n<p>You can see then that thanks to the SMS API library, the minimum SMS handling in JavaScript takes up two a mere two lines of code: <strong>creating the main SMSAPI object<\/strong> and <strong>ordering the HTTP client to send SMS messages<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>const smsapi = new SMSAPI('%SMSAPI_ACCESS_TOKEN%');\nsmsapi.sms.sendSms('+44111222333', 'Hello world!');<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Send messages to multiple recipients \u2013 JavaScript methods<\/h2>\n\n\n\n<p>Let\u2019s move on. Since you intend to use software code to <strong>automate SMS sending to the customers\u2019 phone numbers<\/strong>, you likely need more capabilities. Delivering identical messages to multiple recipients at once? Nothing could be simpler! All you need to do is to add a list of recipients instead of a single number to the same function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>async function SendMultipleAndDisplayStatuses(receivers, message) {\t\nconst response = await smsapi.sms.sendSms(receivers, message);\t\n \t\nfor (const respList of response.list) {\t\n console.log(respList.status);\t\n}\t\n}\n\t\nSendMultipleAndDisplayStatuses(\t\n&#91;'+44111222333', '+44444555666'],\t\n'Hello receivers!'\t\n);<\/code><\/pre>\n\n\n\n<p>You\u2019ll quickly spot the ability to join recipients into groups in the Customer Portal. Our Library in JavaScript allows you to use them. When a whole group is to receive the same content, you just need to use the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>async function SendToGroupAndDisplayStatuses(group, message) {\t\nconst response = await smsapi.sms.sendSmsToGroup(group, message);\n\t\nfor (const respList of response.list) {\t\n console.log(respList.status);\t\n}\t\n}\n\t\nSendToGroupAndDisplayStatuses('group_name', 'Hello group!');<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">If the&nbsp;<code>group<\/code>&nbsp;the argument is a list, everything will execute correctly. The program will simply attempt to send SMS to all recipients from multiple groups.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Additional SMS options: customisation and scheduling<\/h3>\n\n\n\n<p>A message is better received if one feels treated like an individual. Such a&nbsp;minor thing, like addressing someone by their first name, can be the first step towards successful&nbsp;SMS marketing.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/en\/personalization\" target=\"_blank\">Customising<\/a>&nbsp;messages means adding a more personal touch to individual messages. And you don\u2019t have to manually type in every text separately. You can <a rel=\"noreferrer noopener\" href=\"https:\/\/ssl.smsapi.com\/react\/sms-templates\" target=\"_blank\">create content templates<\/a> in the right section of your Customer Panel. Such as this one:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PackageSentTemplate: \u201eHi &#91;%name%], your order has been sent!\u201d<\/code><\/pre>\n\n\n\n<p>The [%name%] element will be replaced with relevant content if you have created a recipient database. It\u2019s a simple way to ensure that <strong>every person receives a message addressed directly to them<\/strong>. Naturally, more field types are available \u2013 we\u2019ve described them all in the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/docs#sms-messages-to-numbers-from-contacts-database-group\" target=\"_blank\">Documentation<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Code snippet: using the SMS message template<\/h4>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>async function NotifyPackageSent(receiver) {\t\nconst details = {\t\ntemplate: 'PackageSentTemplate'\t\n};\n\t\nawait smsapi.sms.sendSms(receiver, \"\", details);\t\n}<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">As you can see, I\u2019ve skipped the message content, as it is included in the template. The&nbsp;<code>details<\/code>&nbsp;variable is of the&nbsp;<a rel=\"noreferrer noopener nofollow\" href=\"https:\/\/github.com\/smsapi\/smsapi-javascript-client\/blob\/master\/src\/modules\/sms\/types\/SmsDetails.ts\" target=\"_blank\"><code>SmsDetails<\/code><\/a> type. It contains a number of additional data, which can be added to the messages to be sent. For example, character coding, date sent, the maximum number of message parts, the template used and custom parameters. You can find a more detailed description in the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/docs\/#2-single-sms\" target=\"_blank\">Documentation<\/a> chapter on sending single messages.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Scheduling a text message in JS<\/h4>\n\n\n\n<p class=\"language-javascript\">Next comes messaging scheduling: the &nbsp;<code>date<\/code>&nbsp;field from the&nbsp;<code>SmsDetails<\/code>&nbsp;object allows you to schedule messages to be sent in the future. It is a standard JavaScript object of the Date type, so you can use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>async function ScheduleSms(receiver, message, futureDate) {\nconst details = {\ndate: futureDate\t\n};\n\t\nawait smsapi.sms.sendSms(receiver, message, details);\t\n}<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">This unassuming function will allow you to&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.smsapi.com\/blog\/how-to-schedule-sms-campaign\/\" target=\"_blank\">schedule SMS campaigns<\/a>! Just in case, you can find in the&nbsp;<code>sms<\/code>&nbsp;module, a function you can use to cancel the scheduled sending of an SMS. This is its signature:<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>async removeScheduledSms(smsId: string | string&#91;]):\nPromise&lt;ScheduledSmsResponse&gt;<\/code><\/pre>\n\n\n\n<p class=\"language-javascript\">The parameter it accepts is the unique identifier that you learn when it is sent. It is returned by the&nbsp;<code>sendSms<\/code>&nbsp;and&nbsp;<code>sendSmsToGroup<\/code>&nbsp;methods \u2013 see above.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SMS sender name<\/h3>\n\n\n\n<p>It is a short <strong>text displayed instead of the sender\u2019s phone number<\/strong>. It allows the recipient to know who\u2019s texting them immediately. It can be your brand name, for example. In SMS marketing, it\u2019s the first opportunity to make a good impression on your customer.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-smsapi-blog wp-block-embed-smsapi-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"kzbGRkMwTn\"><a href=\"https:\/\/www.smsapi.com\/blog\/sender-id-how-to-send-branded-sms-company-name\/\">Sender ID \u2013 How to Send Branded SMS Messages with the Company Name Instead of a Number?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Sender ID \u2013 How to Send Branded SMS Messages with the Company Name Instead of a Number?&#8221; &#8212; SMSAPI Blog\" src=\"https:\/\/www.smsapi.com\/blog\/sender-id-how-to-send-branded-sms-company-name\/embed\/#?secret=qqtaha7dwa#?secret=kzbGRkMwTn\" data-secret=\"kzbGRkMwTn\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>To keep things safe and sound, our employee manually approves each new sender ID during our business hours. You can set sender names in the Customer Portal, and select one of them as the default name.<\/p>\n\n\n\n<p class=\"language-javascript\">From the code perspective, they are handled by the&nbsp;<code>sendernames<\/code> module. It contains several useful methods for searching, creating and deleting sender names. The function below, for example, shows how you can overtly ensure that the text message will be sent with your default SMS sender ID.<\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>async function sendSmsWithDefaultSenderName(receiver, message) {\nconst senderNamesList = await smsapi.sendernames.get();\n\t\nfor (let elem of senderNamesList.collection) {\nif (elem.isDefault) {\nawait smsapi.sms.sendSms(receiver, message, {from: elem.sender});\t\nbreak;\t\n}\t\n}\t\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">SMS sending errors<\/h2>\n\n\n\n<p class=\"language-javascript\">To protect your program from unpleasant surprises, you should include error handling in the code. The Axios (HTTP client) mentioned above returns an error code and message in the event of a sending failure. In such a situation, the SMS API library code will throw an exception with an error object of the <a rel=\"noreferrer noopener nofollow\" href=\"https:\/\/github.com\/smsapi\/smsapi-javascript-client\/blob\/master\/src\/errors\/MessageError\/index.ts\" target=\"_blank\"><code>MessageError<\/code><\/a> type, which contains these two values.<\/p>\n\n\n\n<p>You can find a detailed description of the API request error codes in the&nbsp;<a href=\"https:\/\/www.smsapi.com\/docs\/#18-error-codes\" target=\"_blank\" rel=\"noreferrer noopener\">Documentation<\/a>. Here are some of the most frequently encountered ones:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>11<\/strong> \u2013 text message is too long or special characters are present while the nounicode parameter is used;<\/li>\n\n\n\n<li><strong>12<\/strong> \u2013 message part limit exceeded;<\/li>\n\n\n\n<li><strong>13<\/strong> \u2013 no correct recipient numbers;<\/li>\n\n\n\n<li><strong>14<\/strong>&nbsp;\u2013 incorrect sender name;<\/li>\n\n\n\n<li><strong>26<\/strong>&nbsp;\u2013 message subject is too long;<\/li>\n\n\n\n<li><strong>101<\/strong> \u2013 incorrect authorisation data (incorrect token);<\/li>\n\n\n\n<li><strong>103<\/strong>&nbsp;\u2013 insufficient points on the account to send the message.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Other SMS API modules for JavaScript<\/h2>\n\n\n\n<p class=\"language-javascript\">As I\u2019ve mentioned in the beginning, the library code is divided into modules. Above we\u2019ve learned about&nbsp;<code>sms<\/code>&nbsp;and&nbsp;<code>sendernames<\/code>&nbsp;\u2013 those most important for sending SMS. Some of the other modules are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>templates<\/strong>&nbsp;\u2013 used to handle content and data templates.<\/li>\n\n\n\n<li><strong>contacts&nbsp;<\/strong>\u2013 for creating and editing contacts. Contains minor modules for managing contact groups and custom fields for individual contacts.<\/li>\n\n\n\n<li><strong>mms<\/strong>&nbsp;\u2013 you can use it to send MMS messages in the SMIL format.&nbsp;<\/li>\n\n\n\n<li><strong>profile<\/strong>&nbsp;\u2013 allows you to check your account details in SMSAPI, such as the number of available points, user details, and payment type.<\/li>\n<\/ul>\n\n\n\n<p>If you want to learn more about other aspects of the JavaScript library for sending SMS, I refer you to the <a rel=\"noreferrer noopener nofollow\" href=\"https:\/\/github.com\/smsapi\/smsapi-javascript-client\" target=\"_blank\">GitHub repository<\/a>, and specifically to the files and unit tests. Find them in the individual subfolders, always under the name index.spec.ts. The tests are also examples of how to use the individual methods \u2013 it\u2019s the simplest way to master them. Good luck!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enter the SMSAPI JavaScript library! Sending SMS with JS is simpler than you think. In this programmer\u2019s guide, you\u2019ll find both sample code fragments and a discussion of how to do things and how to tackle errors you may encounter.<\/p>\n","protected":false},"author":21,"featured_media":5234,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[20,39],"class_list":["post-5229","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorial","tag-english","tag-api"],"_links":{"self":[{"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/posts\/5229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/comments?post=5229"}],"version-history":[{"count":22,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/posts\/5229\/revisions"}],"predecessor-version":[{"id":5554,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/posts\/5229\/revisions\/5554"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/media\/5234"}],"wp:attachment":[{"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/media?parent=5229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/categories?post=5229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.smsapi.com\/blog\/wp-json\/wp\/v2\/tags?post=5229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}