GraphQL Email API
Fetch and read emails with GraphQL inboxes using MailSlurp email API.
MailSlurp offers powerful email inbox APIs over REST and GraphQL. The GraphQL API is accessible via HTTP POST
at https://graphql.mailslurp.com
. Let's see how we can use GraphQL to create email addresses, send and even receive emails and attachments using GraphQL.
Authentication
Note you must set an x-api-key
header in your GraphQL client and pass a valid MailSlurp API Key (which you can create for free here). The GraphQL endpoints cover most but not all MailSlurp functionality - for instance file upload/download is exclusive to the REST API and SDK clients.
Explorer
Setup code
You can call MailSlurp's GraphQL email API using any React/GraphQL client. This includes fetch
, curl
etc. An easier way might be using the excellent open source graphql-request
library. First install the NPM dependencies
// npm install --save graphql-request
import { gql, GraphQLClient } from 'graphql-request';
Next we need to create a client that sets the x-api-key
header using to the value of 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,
},
});
Editor config
If you want to enable auto-complete in gql
, VSCode, IntelliJ, or another editor create a .graphqlconfig
file in the root of your project and include this endpoint:
{
"name": "Remote Schema",
"schemaPath": "remote-schema.graphql",
"extensions": {
"endpoints": {
"mailslurp": {
"url": "https://graph.mailslurp.com",
"headers": {
"x-api-key": "YOUR_API_KEY"
},
"introspect": true
}
}
}
}
Query
You can make queries like so. Note that the example gql
method call should use backticks `
to use template string invocation. They are shown below as double quotes "
because of formatting issues. 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
const { createInbox } = await client.request(gql`
mutation {
createInbox {
id
emailAddress
}
}
`);
expect(createInbox.id).toBeTruthy();
expect(createInbox.emailAddress).toContain('@mailslurp');
Send email
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();
Related content
CSharp Email API for DotNET and ASP
Receive email in DotNET Core using C# and MailSlurp
Golang email library
Golang Email Library for sending and receiving emails in Go over SMTP or HTTP/S.
Javascript email libraries for MailSlurp
Nodemailer alternatives to send and receieve email in code and tests
Send and receive email in PHP (without using mail functions)
PHP email API for creating inboxes, sending email, and receiving attachments in code and tests.
Ruby Mailer SDK - send and receive emails in Rails, Rspec an...
Receive email and attachments with Rails, Rspec, and Ruby without SMTP using MailSlurp Ruby Gem.
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?
Hugo responsive image srcsets
Serve responsive picture tags with custom render-image layout partial in Hugo static site generator.
Mailinator alternative
Alternatives to Mailinator for test email accounts. Create real email addresses using MailSlurp
Markdown Heading Sizes
How to specify headers h1, h2, h3 and more in Github flavoured Markdown
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.
Send and receive email in Cypress JS tests with MailSlurp
Test email sign-up. password verification and more with Cypress JS and MailSlurp.
Cypress Email Plugin - Test email accounts in Javascript (an...
Use real email accounts in CypressJS to test user sign-up, email verification, and more.
Test email accounts with Jest and Puppeteer
Test email accounts in React with Jest and Puppeteer. Send and receive emails in Javascript.
Test user sign-up with NodeJS and WebDriver
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 v...
End-to-end testing with MailSlurp, NodeJS, and TestCafe.
Base64 file uploads
How to encode files as Base 64 encoded strings in several languages
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...
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
Testing Email with Cypress JS and MailSlurp
Email testing with Cypress JS