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/) API Key (which you can [create for free here](https://app.mailslurp.com)). The GraphQL endpoints cover most but not _all_ MailSlurp functionality - for instance file upload/download is exclusive to the [REST API](/docs/api/) and [SDK clients](/developers/).

## Explorer

[Open GraphQL explorer](https://graph.mailslurp.com)

## Setup code

You can call [MailSlurp's GraphQL email API](https://graphql.mailslurp.com) 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



```typescript
// uses the native fetch API built into Node.js
import { getLogger } from "../lib/logger";
```



Next we need to create a client that sets the `x-api-key` header using to the value of your [MailSlurp account API KEY](https://app.mailslurp.com/sign-up/).



```typescript
// create a helper that sends GraphQL POST requests to the MailSlurp endpoint
// and passes your MailSlurp API key in the x-api-key header
```



### Editor config

If you want to enable auto-complete in `gql`, VSCode, IntelliJ, or another editor create a `.graphqlconfig` file [in the root](https://graphql-config.com/introduction/) of your project and include this endpoint:

```json
{
  "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.



```typescript
const query = `
  {
    inboxes {
      numberOfElements
    }
  }
`;
const { inboxes } = await graphqlRequest<{
  inboxes: { numberOfElements: number };
}>(query);
expect(inboxes.numberOfElements).toBeGreaterThan(0);
```



The great thing about graphql is that you can explore the MailSlurp API schema yourself using the provided [GraphQL Playground](https://graphql.mailslurp.com)

## Mutations



```typescript
const { createInbox } = await graphqlRequest<{
  createInbox: { id: string; emailAddress: string };
}>(`
  mutation {
    createInbox {
      id
      emailAddress
    }
  }
`);
expect(createInbox.id).toBeTruthy();
expect(createInbox.emailAddress).toContain("@");
```



### Send email



```typescript
const { sendEmail } = await graphqlRequest<{
  sendEmail: { id: string };
}>(
  `
    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();
```


