Skip to main content

Part 1 - Setup

Once you complete this section, you will have created a Mailchain account that you will use for this workshop and for your own development and testing.

caution

NOTE ON SECURITY: It’s a good idea to test your code using a non-production account. It’s also important to ensure your account details are never saved in a public place or committed to your git repository. For example, append -mc-wrkshp to your preferred username.

Create application

You will use this application to complete the workshop.

Open your terminal window to create a new directory that we will store your sample code in, then navigate into it.

mkdir mailchain-workshop
cd mailchain-workshop

Use npm to create a new package.json file.

npm init --yes

Initialize a git repository so we can commit your code from time to time and roll back if necessary.

git init

Now you have an empty application that you can start writing code in.

Install SDK

Now we can install the Mailchain SDK.

npm install @mailchain/sdk

Configure SDK

We can now configure your application to use the Mailchain SDK to authenticate with the Mailchain protocol.

Run the following command to create a gitignore file for for Node. .gitignore files prevent you from unintentionally committing .env files where you will store secreted including your SECRET_RECOVERY_PHRASE.

curl -o .gitignore https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
danger

You must not commit .env to git for this workshop because it will contain your SECRET_RECOVERY_PHRASE.

Use dotenv to pass the contents of .env as environment variables to your application. The dotenv package enables you to add a file called .env to the root of the project that contains environment variables that are then passed to your application when it's ran. Install dotenv by running the following command:

npm install dotenv

Add your SECRET_RECOVERY_PHRASE to .env. Open .env and add your 24 word phrase, it should look similar to below.

SECRET_RECOVERY_PHRASE="your secret recovery ... phrase"
note

Your Secret Recovery Phrase is a 24 word mnemonic phrase that is securely generated for when you create an account. At any time can access your Secret Recovery Phrase via the settings page.

Authenticate

Now you have installed and configured the SDK you can test the authentication by getting the user profile for the supplied Secret Recovery Phrase.

Paste the following code into auth.js.

// import Mailchain SDK
const { Mailchain } = require('@mailchain/sdk');
// import .env environment variables
require('dotenv').config();

// 24 word secret recovery phrase
const secretRecoveryPhrase = process.env.SECRET_RECOVERY_PHRASE;
// create a new instance of the Mailchain SDK with the secret recovery phrase
const mailchain = Mailchain.fromSecretRecoveryPhrase(secretRecoveryPhrase);

async function main() {
const user = await mailchain.user();
console.log(user);
}
main();

In your terminal window run node auth.js to start the application. You should an output similar to below:

{
address: 'xxxxxx-mc-workshop@mailchain.com',
username: 'xxxxxx-mc-workshop'
}

You have successfully setup and authenticated the Mailchain SDK, you can move on to the next step.