GraphQL Email API Tutorial

Did you know you can send and receive emails using GraphQL?

GraphQL is a hot new technology for fetching data using a graph. MailSlurp offers a free email address API that you can use to create inboxes, read email, send emails and more. Let's see how it works.

Where is it?

Access the MailSlurp GraphQL email API endpoint at https://graphql.mailslurp.com. If you load the url in a browser you'll see the GraphQL explorer. You can use it to perform queries or use your own graphql client locally.

Install GraphQL client

You can install MailSlurp's graphQL library using NPM:

// npm install --save graphql-request
import { gql, GraphQLClient } from 'graphql-request';

Note you will need a free MailSlurp API key. Create an API key at MailSlurp account dashboard and store the value safely. Then use the key in code to configure the client. We must create a client that sets an x-api-key header using your MailSlurp account API KEY.

// create a new graphql-request client using the MailSlurp graphql endpoint
// and passing a headers map including your MailSlurp API Key using "x-api-key" header
const client = new GraphQLClient('https://graphql.mailslurp.com', {
  headers: {
    'x-api-key': YOUR_API_KEY,
  },
});

Making a query

You can make queries like so. Note that the example gql method call should use backticks ` to use template string invocation. Note you can use raw strings without gql too if you prefer.

const query = gql`
  {
    inboxes {
      totalElements
    }
  }
`;
const { inboxes } = await client.request(query);
expect(inboxes.totalElements).toBeGreaterThan(0);

The great thing about graphql is that you can explore the MailSlurp API schema yourself using the provided GraphQL Playground

Mutations

Here is how you can perform mutations.

const { createInbox } = await client.request(gql`
  mutation {
    createInbox {
      id
      emailAddress
    }
  }
`);
expect(createInbox.id).toBeTruthy();
expect(createInbox.emailAddress).toContain('@mailslurp');

Send email

And here is a way to send emails using GraphQL.

const { sendEmail } = await client.request(
  gql`
    mutation SendEmail(
      $fromInboxId: String!
      $to: [String!]!
      $subject: String!
    ) {
      sendEmail(fromInboxId: $fromInboxId, to: $to, subject: $subject) {
        id
      }
    }
  `,
  {
    fromInboxId: createInbox.id,
    to: [createInbox.emailAddress],
    subject: 'Test',
  }
);
expect(sendEmail.id).toBeDefined();

Further reading

For more information please see the GraphQL email guide.

Related content

Java email library for SMTP and creating email addresses

MailSlurp Java SDK for sending and receive email and attachments on the JVM.

NodeJS MailSlurp SDK

Nodemailer alternatives to send and receieve email in code and tests

Email for testing

Test email accounts for email testing. Alternatives to Mailinator, MailTrap, Mailosaur and more.

Email marketing campaign bulk email testing

Send bulk transactional emails with MailSlurp online or in code using CSharp, Node.js, Java, PHP and more.

Cold Emailing in Compliance with CAN-SPAM

Cold Emailing in Compliance with CAN-SPAM

Do's and Don'ts for multiple SPF records

Do's and Don'ts for multiple SPF records

Email Over Blockchain

Can emails be sent over blockchain? Well kind of! LedgerMail and other and at the cutting edge of decentralized email.

Explaining Cloud Email with Options to Think About

Explaining Cloud Email with Options to Think About

Fastest way to start a typescript project

Modern typescript tooling has come a long way. See how to setup a new project with TS-Node, TSC, and typeconfig.json.

Fetch error message javascript

Handle response exceptions with Fetch in Javascript

GraphQL API disposable email accounts

How to create real email addresses using GraphQL to send and receive emails in tests and frontend applications.

GraphQL Email API Tutorial

Did you know you can send and receive emails using GraphQL?

How to choose the right email design size (for various campaigns)

Email sizes can be instrumental to determine if your consumer will comprehend and engage with your content. While the template width (600 px) is typical, there are nuances to be mindful of. There are no hard and fast standards for email template height like design blocks.

HTML Mailto Links Explained

HTML Mailto is uncomplicated to use. This link opens the default email client and adds a new notification maker. Pre-populated fields save time and prevent email bounces. This post will cover customizing mailto. We'll also discuss its viability in this decade. Come on.

Hugo responsive image srcsets

Serve responsive picture tags with custom render-image layout partial in Hugo static site generator.

How to send emails in Javascript (NodeJS)

JS SMTP email sending guide using HTML and NodeJS.

Mailinator alternative

Alternatives to Mailinator for test email accounts. Create real email addresses using MailSlurp

How to use Mailto links

Mailto links not working guide.

Manage Email Throttling Correctly!

Manage Email Throttling Correctly!

Maximum Email Size and How to Optimize It

Email sizes are fundamental for a few important reasons. One is security, and another is the performance of your email.

NodeMailer NPM Tutorial

Send and receive email using NodeMailer in Node JS.

Create custom print classes with Tailwind

Configure tailwind.config.js to create prefixed classes with a custom media query.

Transactional Email Services Compared

Sending emails is important for many applications. Top 10 transactional email services compared.

What is DKIM Email

How to setup and configure DKIM signatures with the sender policy framework (SPF)

CypressJS Example

Test email sign-up. password verification and more with Cypress JS and MailSlurp.

CypressJS Email Testing

Use real email accounts in CypressJS to test user sign-up, email verification, and more.

Jest, Puppeteer

Test email accounts in React with Jest and Puppeteer. Send and receive emails in Javascript.

Webdriver, JS, WDIO

Test email related processes like sign-up and verification using WDIO WebDriver and MailSlurp.

TestCafe end-to-end MFA testing for user sign-up and email verification

End-to-end testing with MailSlurp, NodeJS, and TestCafe.

Base64 file uploads

How to encode files as Base 64 encoded strings in several languages

CSharp Email Tutorial

SMTP mailserver testing and usage in CSharp using

Deno Email Apis

Send and receive emails in Deno JS. Use APIs in Deno with MailSlurp.

Email read (opened seen settings)

How to control the seen or read settings for an email using MailSlurp.

GraphQL Email API

Fetch and read emails with GraphQL inboxes using MailSlurp email API.

Testing OTP password link username and password for 2 factor authentication (2FA)

Testing OTP password link username and password for 2 factor authentication (2FA)

MailSlurp NodeMailer SMTP Usage

Use SMTP nodemailer with MailSlurp disposable email addresses

Testing email with Cypress test email accounts

Test email accounts for CypressJS. End-to-end testing with real email addresses using MailSlurp Cypress plugin.

Send emails in NodeJS using SMTP

How to use Javascript SMTP client (or Nodemailer) to send email with MailSlurp mail server

Email internationalization and i18n localization

Email internationalization and i18n localization

Testing Email with Cypress JS and MailSlurp

Email testing with Cypress JS

Ready to dive in?Start building email applications today.