import fetch from 'node-fetch'; async function run() { const resp = await fetch( `https://api.mailgun.net/v1/keys`, { method: 'GET', headers: { Authorization: 'Basic ' + Buffer.from('<username>:<password>').toString('base64') } } ); const data = await resp.text(); console.log(data); } run();
{- "items": [
- {
- "created_at": "2006-01-02T15:04:05",
- "description": "api key",
- "id": "e2153fd0-e0277777",
- "kind": "user",
- "requestor": "janedoe@example.com",
- "role": "admin",
- "updated_at": "2006-01-02T15:04:05",
- "user_name": "Jane Doe",
- "domain_name": null
}
], - "total_count": 1
}
Create Mailgun API key
import FormData from 'form-data'; import fetch from 'node-fetch'; async function run() { const form = new FormData(); form.append('role','string'); const resp = await fetch( `https://api.mailgun.net/v1/keys`, { method: 'POST', headers: { Authorization: 'Basic ' + Buffer.from('<username>:<password>').toString('base64') }, body: form } ); const data = await resp.text(); console.log(data); } run();
{- "key": {
- "description": "api key",
- "kind": "domain",
- "role": "sending",
- "created_at": "2006-01-02T15:04:05",
- "updated_at": "2006-01-02T15:04:05",
- "domain_name": "example.com",
- "id": "f2153fd0-f1277777",
- "user_name": null,
- "requestor": null
}, - "message": "great success"
}
import fetch from 'node-fetch'; async function run() { const keyId = 'YOUR_key_id_PARAMETER'; const resp = await fetch( `https://api.mailgun.net/v1/keys/${keyId}`, { method: 'DELETE', headers: { Authorization: 'Basic ' + Buffer.from('<username>:<password>').toString('base64') } } ); const data = await resp.text(); console.log(data); } run();
{- "message": "key deleted"
}
Regenerate Mailgun Public API key
import fetch from 'node-fetch'; async function run() { const resp = await fetch( `https://api.mailgun.net/v1/keys/public`, { method: 'POST', headers: { Authorization: 'Basic ' + Buffer.from('<username>:<password>').toString('base64') } } ); const data = await resp.text(); console.log(data); } run();
{- "key": "pubkey-626b31f321228ddddddddddcc5a7a1c9",
- "message": "The public API key has been successfully regenerated."
}