Sending your first e-mail

Introduction

This guide will run you through sending your first e-mail that contains an action. An action is a button (or really a link) inside your e-mail, such as "Click to sign up to AirBnB for Cats".You send e-mails in Magic Login through a JSON HTTP API.
Let's look at an example API request to the SendTemplateEmail endpoint:
Where possible, Magic Login's API supports Markdown.
1
{
2
"from": "AirBnB for Cats <[email protected]>",
3
"reply_to": "Cat Jones <[email protected]>",
5
"subject": "Please verify your e-mail to sign up",
6
7
"content": {
8
"main": "# Welcome\nClick the button to sign up to AirBnB for cats.",
9
"action": {
10
"name": "Create Account",
11
"url": `https://airbnbforcats.com/magic`,
12
"purpose": "signup",
13
},
14
"outro": "Have a purrfect day,\nThe AirBnB for Cats team",
15
"sub": "If you did not intend to sign up you can ignore this e-mail.",
16
"footer": "AirBnB for Cats Β© 2021 - [Terms & Privacy](https://airbnbforcats.com/terms)"
17
}
18
}
Copied!
Screenshot of the resulting e-mail

Making the actual request

We will make a HTTP POST request tohttps://api.magiclogin.net/<YOUR-APP-ID>/v1/sendTemplateEmail.
To authenticate you are required to send a header with name Authorization and valueBearer <YOUR-API-TOKEN>.
Replace <YOUR-APP-ID> with the Application ID and replace <YOUR-API-TOKEN>by the API Token we created during setup.
Reminder: the API Token is a secret, don't put it in your front-end code. You should only make requests to the Magic Login API from your server backend, never your front-end client.
You will be able to make this request from any programming language, here's a Node.js example that uses the popular axios library:
1
const axios = require("axios");
2
​
3
const apiToken = "apitoken_secretsecretsecretsecret";
4
const appID = "app_eXAmple";
5
​
6
const url = `https://api.magiclogin.net/v1/${appID}/sendTemplateEmail`;
7
const headers = {
8
"Authorization": `Bearer ${apiToken}`
9
};
10
const body = {
11
"from": "AirBnB for Cats <[email protected]>",
12
"reply_to": "Cat Jones <[email protected]>",
13
14
"subject": "Please verify your e-mail to sign up",
15
"content": {
16
"main": "# Welcome\nClick the button to sign up to AirBnB for cats.",
17
"action": {
18
"name": "Create Account",
19
"url": `https://airbnbforcats.com/magic`,
20
"purpose": "signup",
21
},
22
"outro": "Have a purrfect day,\nThe AirBnB for Cats team",
23
"sub": "If you did not intend to sign up you can ignore this e-mail.",
24
"footer": "AirBnB for Cats Β© 2021 - [Terms & Privacy](https://airbnbforcats.com/terms)"
25
}
26
}
27
​
28
axios.post(url, body, {headers: headers})
29
.then(response => {
30
console.log(`status: ${response.status}`)
31
console.log(response.data)
32
})
33
.catch(error => {
34
console.log(error);
35
console.error(`Something went wrong: ${JSON.stringify(error.response.data)} (status ${error.response.status})`)
36
});
Copied!
When we run above code, it prints:
1
status: 200
2
{
3
success: true,
4
data: {
5
email: {
6
from: 'AirBnB for Cats <[email protected]>',
7
reply_to: 'Cat Jones <[email protected]>',
9
id: '01020177a8dc234d-51955b92-546f-4da8-9e28-3e7a8eb411ea-000000'
10
},
11
token_id: 'GdyU4bHKeRDYxjrn7y4Fk576HHDpCLyaoNYRUgAtKKGf'
12
}
13
}
Copied!
That's it! We just sent our first e-mail using the Magic Login API πŸŽ‰. For more details check out the API page on Sending Emails:

What happens now?

Our recipient [email protected] will have received an e-mail with a big button that says Create Account. This button links to something like https://airbnbforcats.com/magic?mtoken=9nwTWNkNDG1TgwaNzVAt3t. This mtoken query parameter automatically got added to the URL. This is a secret token that we will verify in the next guide, which will allow us to authenticate the recipient of the e-mail.
​
Last modified 11mo ago