-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
23 lines (22 loc) · 1.19 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
async function postData(data = {}) {
// Default options are marked with *
let url = 'https://api.sendgrid.com/v3/mail/send';
const response = await fetch(url, {
method: 'POST',
mode: 'no-cors', // no-cors, *cors, same-origin
headers: {
'Content-Type': 'application/json',
'Authorization': "Bearer $KEY"
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
console.log(response);
return response.json(); // parses JSON response into native JavaScript objects
}
var dataemail = { "personalizations": [{ "to": [{ "email": "[email protected]" }] }], "from": { "email": "[email protected]" }, "subject": "Sending with SendGrid is Fun", "content": [{ "type": "text/plain", "value": "and easy to do anywhere, even with cURL" }] }
postData(dataemail)
.then(data => {
console.log(data); // JSON data parsed by `data.json()` call
});