Why am I getting unauthorized error from Mailgun?

181 views Asked by At

I'm trying to get start with Mailgun but i'm getting the error:

[Error: Unauthorized] {

status: 401,

details: 'Forbidden',

type: 'MailgunAPIError'

}

is this some user's settings? I did copy both the private and public key, and the domain, from dashboard. I don't know what's wrong.

from code:

const formData = require('form-data');
const Mailgun = require('mailgun.js');
const mailgun = new Mailgun(formData);
const mg = mailgun.client({
username: 'api',
key: '5d....',
public_key: 'pubkey-....'
});
mg.messages.create('sandboxyyyyyyyy.mailgun.org', {
from: "Excited User [email protected]",
to: ["[email protected]"],
subject: "Hello",
text: "Testing some Mailgun awesomness!",
html: "<h1>Testing some Mailgun awesomness!</h1>"
})
.then(msg => console.log(msg))
.catch(err => console.log(err));
1

There are 1 answers

0
Manel On

Your credentials looks wrong.

Doc says :

  const mg = mailgun.client({username: 'api', key: process.env.MAILGUN_API_KEY || 'key-yourkeyhere'});

So in your case should be :

  const mg = mailgun.client({username: '5d....', key: '...'});

There is no public_key property. And if you use that key you must prefix it with "key-" not "pubkey-".