How to Use Your API Key
To start using Notify to send emails programmatically, you’ll need to securely integrate your API key into your project. This guide will show you how to access your API key and use it to send emails with minimal code.
Accessing Your API Key
When you first log into the Notify dashboard, you’ll be prompted to create a company profile. Once your company is created, navigate to the Credentials page to access your API key.
- Go to Credentials in your dashboard.
- Copy the API key provided. For security, you won’t be able to view this key again, so store it in a secure location.
⚠️ Keep your API key secure. Treat it like a password, and do not expose it in client-side code or share it publicly.
Integrating Your API Key
Here’s how to use your API key to send emails with Notify.
Using the Notify NPM Package
Notify provides an npm package for easy integration:
- First, install the package:
npm install notify
- Import and initialize Notify in your project, passing in your API key:
import Notify from 'notify';
const notify = new Notify('<your_api_key>');
const sendEmail = async () => {
try {
await notify.send({
to: 'recipient@example.com',
name: 'Recipient Name',
template_id: 'welcome-template',
data: {
name: 'Recipient Name',
company: 'Your Company',
message: 'Welcome to our service!',
},
});
console.log('Email sent successfully!');
} catch (error) {
console.error('Failed to send email:', error);
}
};
sendEmail();
- Customize the send method parameters:
to: Recipient's email address name: Recipient's name template_id: The template ID you created in the Notify dashboard data: Any custom fields required for the email template (e.g., name, company, message) 💡 Note: Notify will automatically match your data fields to placeholders in your email template, allowing for dynamic content.
Using the Notify API Directly Alternatively, you can call the Notify API directly from your server without the npm package using Node.js's native fetch.
Set up a POST request to the Notify endpoint:
const sendEmail = async () => {
try {
const response = await fetch('https://api.notify.com/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer <your_api_key>`,
},
body: JSON.stringify({
to: 'recipient@example.com',
template_id: 'welcome-template',
data: {
name: 'Recipient Name',
company: 'Your Company',
},
}),
});
if (!response.ok) {
throw new Error('Failed to send email');
}
console.log('Email sent successfully!');
} catch (error) {
console.error('Error sending email:', error);
}
};
sendEmail();
- Customize the payload to match your template and data needs, as shown in the example above.
Best Practices
Store Your API Key Securely: Avoid hard-coding your API key in your source files. Instead, store it in environment variables or a secure vault.
Rotate Your API Key Regularly: For added security, consider regenerating your API key periodically.
Troubleshooting
If you encounter issues, here are a few common checks:
- Invalid API Key Error: Ensure your API key is correct and hasn't been revoked.
- Rate Limits: Free-tier users have monthly email limits. Check your usage in the Notify dashboard under Usage & Limits.
- For more detailed usage limits and restrictions, refer to the Notify API Documentation or reach out to our support team.