Api sendgrid com v3 mail send
Here is an example of how to send an email using the SendGrid API v3:
Step 1: Create a SendGrid account and obtain an API key
- Go to the SendGrid website and sign up for an account.
- Once you have an account, go to the "Settings" page and click on "API Keys".
- Click on the "Create API Key" button and give your API key a name.
- Copy the API key and store it securely.
Step 2: Install the SendGrid API client library
- You can install the SendGrid API client library using npm or yarn:
npm install @sendgrid/mail
- Alternatively, you can use the SendGrid API client library for your preferred programming language.
Step 3: Set up your SendGrid API credentials
- Create a new file called
sendgrid.js
and add the following code:const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
* Replace `process.env.SENDGRID_API_KEY` with your actual SendGrid API key.
**Step 4: Send an email using the SendGrid API**
* Create a new file called `send-email.js` and add the following code:
const sgMail = require('@sendgrid/mail');
const msg = { to: '[email protected]', from: '[email protected]', subject: 'Hello from SendGrid!', text: 'Hello from SendGrid!', html: 'Hello from SendGrid!' };
sgMail.send(msg);
* Replace `[email protected]` and `[email protected]` with the actual email addresses you want to use.
**Step 5: Run the script**
* Run the `send-email.js` script using Node.js:
node send-email.js
* The email should be sent successfully using the SendGrid API.
Here is the complete code:
// sendgrid.js const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
// send-email.js const sgMail = require('./sendgrid');
const msg = { to: '[email protected]', from: '[email protected]', subject: 'Hello from SendGrid!', text: 'Hello from SendGrid!', html: 'Hello from SendGrid!' };
sgMail.send(msg);
Make sure to replace the `SENDGRID_API_KEY` environment variable with your actual SendGrid API key.