Sending Email using NodeJS axios

May 04, 2021
With Emailkick, you no longer need to write email HTML within your application code. Emailkick provides a mordern template builder to help you design good looking emails. To send the email to an email address, use the Emailkick Send API. Every email will have it's own unique ID to keep track of the emails.

In this guide we are explaining,

  1. Prerequisites
  2. Sending using API
  3. Working with variables
  4. Handling API responses
  5. Understanding Error Codes

Prerequisites for sending your first email with the Emailkick API

Before you can start using the API, you need to do the following:

  1. Create a Emailkick account.
  2. Create an API Key and Access ID.

Sending using API

Your API call must have the following components:

  • A host. The host for emailkick is https://api.emailkick.com/api/mail/send
  • Request method set to POST
  • A request payload. the email payload is a structured JSON which has data like to, from, templateId and the authorization keys.

A NodeJS function request using fetch looks like this

var axios = require('axios'); function sendEmail({ templateId, variables, to, from }) { const APIKEY = "<<YOUR EMAILKICK API KEY>>"; const ACCESSID = "<<YOUR EMAILKICK ACCESS ID>>"; var raw = JSON.stringify({ "APIKEY": APIKEY, "ACCESSID": ACCESSID, "to": to, "from": from, "templateId": templateId, "variables": variables }); var requestOptions = { method: 'POST', headers: { "Content-Type", "application/json" }, body: raw, url: 'https://api.emailkick.be/mail/send/' }; axios(config) .then(function (response) { //do something console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); }); } sendEmail({ to: 'elon@emailkick.com', from: 'yourname@yourdomain.com', templateId: '<<Emailkick Template Id>>' });

Understanding the request payload

Request Payload required following elements

  • APIKEY Required - Your Emailkick API Key. You get the key from your Emailkick dashboard > Settings > Emailkick API key.
  • ACCESSID Required - Your Emailkick Access ID. You get the key from your Emailkick dashboard > Settings > Emailkick API key.
  • to Required - The to recipient email address you want to send the email to.
  • from Required - Sender email address you are sending from. This email address should be same as the email you registered

with Emailkick and Amazon SES.

  • templateId Required - The id of the email design template. You will get the design template ID from your Emailkick dashboard.
  • variables - This data is used to replace the variables you have set on the templates. if a variable is defined on the design but not on the API payload, it will be replaced with blank space.

Handling API responses

Once you trigger the Send API, your email will will be added to a queue which will be then processed by SES mail server.

If the payload and secret keys are correct, you will get a success as true result with 200 OK response code.

In case of an error the following table will help you identify the issue

Reponse Code Error Code Reason
200 - Your email is now processing and sending to the recipient ✅
401 EK_0401 You are unauthorized to process this request. This is because your API Key is wrong
403 EK_0403-2 You are sending to an unsubscribed user.
404 EK_0404 Requested template ID is not found
404 EK_0404-1 Incorrect Access ID / A user of this access ID might not exist
405 EK_0405-1 To email address is missing on your request payload
405 EK_0405-2 From email address is missing on your request payload
405 EK_0405-3 Design Template ID is missing on your request payload
405 EK_0405-4 Access ID (ACCESSID) is missing on your request payload
405 EK_0405-5 API Key (APIKEY) is missing on your request payload
405 EK_0405-6 To email address format is incorrect. Check the validity of the email address
500 EK_500 An issue with Emailkick server. ⚠️