This tutorial shows you how to integrate an API that you can use to send messages from your app to any EVM-compatible blockchain address, ENS name or Unstoppable domain.
Through a practical example, you’ll learn how to build an Express App that exposes an API you can use to send messages with Mailchain.
Your app will use the Mailchain SDK that handles signatures, encryption, and sending the message. In most cases, you would add a route to an existing Express App, but for this tutorial, you'll create a new app.
For more detailed instructions see the written tutorial here. You can find the final code and working example in mailchain/examples-js on GitHub.
Step 1: New API server: You’ll create a new Express app that exposes an API.
Step 2: Mailchain SDK: You’ll install and configure the Mailchain SDK which handles all the sending and encryption.
Step 3: ‘Send’ service: Express uses services to perform tasks. You’ll create a service for sending mail using the Mailchain SDK.
Step 4: Mail controller: Controllers manage HTTP requests and send the necessary data to the correct service. You’ll create a Mail controller to manage the HTTP request and response.
Step 5: Wire up the API: You’ll create an additional MailRoute and add it to the server.
Step 6: Test: You’ll send a message from your app to your Mailchain account.
To complete this example, you need to first:
The simplest way to create a new Express app that exposes an API is with a generator that creates an Express App structure. There are various express generators, for this tutorial you'll use typescript-express-starter.
To begin, open a terminal window on your computer and run:
Select the following responses: default; n; y (as shown below)
Your Express App is now created.
Open mailchain-send-api in your favorite code editor. You'll see it contains example services, controllers, routes, etc.
The Mailchain SDK is the easiest way to use Mailchain, it deals with all of the encryption, signatures, and hashing for you.
To install the Mailchain SDK you need to run an npm command in the base directory of your project mailchain-send-api. If you are following Step 1, you should already be in this folder. Otherwise, open up a terminal window and navigate to your mailchain-send-api folder.
Install Mailchain SDK using npm:
You have now installed the Mailchain SDK and are ready to configure it.
The Mailchain SDK needs to be authenticated with your Secret Recovery Phrase to send messages. We are assuming you are developing on a local machine and it is secure.
Whoever has the Secret Recovery Phrase controls the Mailchain account. Secret Recovery Phrases MUST be kept safely and only saved in a trusted store. We suggest for this tutorial that you use a Secret Recovery Phrase from a test account. Go ahead and create a new account if you don't already have one.
Get the Secret Recovery Phrase for your test development account (see WARNING). Open .env.development.local in your editor, and add a new line to the bottom
Replace enter your secret phrase here with your Secret Recovery Phrase and save the file.
In your terminal window run, npm run dev from the root directory of your mailchain-send-api app. You should get a response to say the app successfully started, e.g. App is listening on port 3000.
The service (MailService) will:
Inside src/services/ create mail.service.ts and copy the code below into it.
MailController will perform the following actions:
In your editor create mail.controller.ts in the src/controllers/ folder.
Copy and paste the code below into it.
The API is called by clients using HTTP. You'll create an additional route that accepts mail parameters. The route will use the sendMail function you created in Step 3.
MailRoute performs the following actions:
In your editor create mail.route.ts in the src/routes/ folder. Copy and paste the code below into it.
Now MailRoute has been created, the final code change is to add MailRoute to the server.
In your editor open the existing server.ts file in the src/ folder.
Replace the existing contents by copying and pasting the code below into it.
Now your app is ready to start accepting HTTP requests.
Now you have API that can send messages. You can test this by sending a sample request via HTTP.
Open a new terminal window, and paste the following curl command into a terminal window.
You should get a {"status": "success"} response.
You can check the message has been sent by looking in the sent folder in the Mailchain web app.
Want to try more? Edit the curl command above and give these go:
Congratulations 🎉 you have built an App that exposes an API. You can use this API to send messages from other services and apps, for example messaging your users about exciting updates.