Trigger notifications with the API

Quick guide on how to send notifications to wallet addresses using the hashmail dapp API

📋Prerequisites

Before we begin calling the API to send a message, you need to setup your dapp account on the hashmail console, and generate an API Token

🚧

an API token is mandatory to send messages using the hashmail API

To generate your api Token please setup your account on the hashmail console


📩Making an API request to send a message

In this section, we will look into how we can make an API request that sends a notification to the specified wallet adresses.

Step 1 - Add the Authorization Token in the Headers

After obtaining the Authorization Token, we need to add it to the headers of the request made. To do that, we pass the Authorization Token in the request headers object as a value to the Authorization key.

// step 1 - replace your api token below. To get one, login to the hashmail console at console.hashmail.dev
const AuthorizationToken = "<your-api-token>";
var myHeaders = new Headers();
myHeaders.append("Authorization", `Bearer ${AuthorizationToken}`);

With this snippet we have successfully created our requests headers with the Authorization Token present in them.

The next steps will be to create the body of the request.

Step 2 - Creating the body of the request

The body data is to be sent in a FormData. This FormData will have multiple key-value pairs in it, listed as follows:

KeyData TypeRequiredDescriptionExample
sender_addressstringtrueWallet address of your dapp"0x83C1B24e8CB73D165787dcEb9A8B2D8A6d5188fE"
to_addressstringif cc_address and bcc_address are empty: trueComma-separated values of wallet address"0x83C1B24e8CB73D1657shdcEb9A8B2D8A6d5188fE", "0x93C1B24eYCB73D1657shdcEb9A8B2K8A6d5188fE"
subjectstringfalsesubject of the message"Sending mails over the web3 is possible"
contentstringfalsebody of the message"HTML is also supported"
message_previewstringfalse140 characters long preview of the message"HTML is also supported"

The recipients of the mail (to_address) can be one of the following:

  • [wallet-address]@hashmail.dev
  • [ENS]@hashmail.dev
  • [Unstopabble Domain]@hashmail.dev
  • [Lens ID]@hashmail.dev
  • [sol name]@hashmail.dev
  • Any web2 mailing service such as an @gmail.com
// step 1
const AuthorizationToken = "<your-authorization-token>";
var myHeaders = new Headers();
myHeaders.append("authorization", `Bearer ${AuthorizationToken}`);

// step 2
var formdata = new FormData();
formdata.append("sender_address", "n144m1MFdqddkTcNUzA6CYwo7hAQjt4E4z");
formdata.append("to_address", "john_doe.eth, wallet-address-1");
formdata.append("subject", "swapnika.eth");
formdata.append("content", "swapnika.eth");

Step 3 - Creating the request

The final step is creating the desired call. By this step we have a FormData payload that we want to attach to our request. It has all the information about the message. Along with that, we have an Authorization Token that authorizes the call made.

The call made is a POST method. As an example, we have used fetch API below to call the request.

// step 1
const AuthorizationToken = "<your-authorization-token>";
var myHeaders = new Headers();
myHeaders.append("authorization", `Bearer ${AuthorizationToken}`);

// step 2
var formdata = new FormData();
formdata.append("sender_address", "n144m1MFdqddkTcNUzA6CYwo7hAQjt4E4z");
formdata.append("to_address", "john_doe.eth, wallet-address-1"]);
formdata.append("subject", "swapnika.eth");
formdata.append("content", "swapnika.eth");

// step 3
var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: formdata,
  redirect: 'follow'
};
fetch("https://api.hashmail.dev/dapp/messages/send", requestOptions)
  .then(response => response.json())
  .then(result => console.log(result))
  .catch(error => console.error(error));

✅That's it! hashmail will deliver the message to users

hashmail will deliver the notification across mutiple channels including:

  1. Your dapp front-end, in the hashmail widget
  2. Email address
  3. Telegram DM
  4. Discord DM
  5. hashmail web3 inbox for users

What’s Next

Read more about the hashmail inbox for users