-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathnode-calls-fetch.js
39 lines (34 loc) · 1.07 KB
/
node-calls-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
/**
* Making phone calls 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 folloing command:
* node --experimental-fetch node-send-sms-fetch.js
*
* node-fetch module:
* Since v3 node-fetch does no longer support require().
* To use 'import', remember to add "type":"module" in your package.json.
*
*/
import fetch from "node-fetch"; // v.3
// const fetch = require("node-fetch"); // v.2
// API credentials
const username = '<API Username>';
const password = '<API Password>';
const authKey = Buffer.from(username + ":" + password).toString("base64");
// Request data object
var data = {
from: "+46700000001",
to: "+46700000002",
voice_start: '{"connect":"+46700000003"}'
}
data = new URLSearchParams(data);
data = data.toString();
fetch("https://api.46elks.com/a1/calls", {
method: "post",
body: data,
headers: {"Authorization": "Basic " + authKey}
})
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.log(err))