-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathnode-send-sms-fetch.js
40 lines (35 loc) · 1.04 KB
/
node-send-sms-fetch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Sending SMS with fetch
*
* 2022-02-24: At the time, fetch() is still an experimental feature in node (since v.17.5).
* To run this code without the node-fetch module, use the following command:
* node --experimental-fetch node-send-sms-fetch.js
*
* Since v3 node-fetch does no longer support require().
* To use 'import', remember to add "type":"module" in your package.json.
*
* For node-fetch v.2, use:
* const fetch = require("node-fetch")
*
*/
import fetch from "node-fetch";
// API credentials
const username = "<API Username>";
const password = "<API Password>";
const authKey = Buffer.from(username + ":" + password).toString("base64");
// Request data object
var data = {
from: "NodeElk",
to: "+46700000001",
message: "Hej Vad trevligt att se dig!"
}
data = new URLSearchParams(data);
data = data.toString();
fetch("https://api.46elks.com/a1/sms", {
method: "post",
body: data,
headers: {"Authorization": "Basic " + authKey}
})
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.log(err))