-
SummaryI want to get info from SendGrid and receive it on my page with a POST request. Testing it with Postman would go some like this here: ^^ replace the URL with my URL email.tu.biz/api/sendgrid ^^ it says the source didn't support that method. Is the code OK to work with Sendgrid?
Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Wait, so, you go to SendGrid, and you say, when an email is received, you can use this URL, and this URL is Have you deployed this? is the latest version of your application, containing this change deployed? One thing you have to fix is: // Delete this config - as it is written- it doesn't work in App Router
export const config = {
api: {
bodyParser: false,
},
}; Change this: // Parse the multipart/form-data from SendGrid
const fields = await parseForm(req);
// Destructure email fields from the request
const { from, to, subject, text, html } = fields; to: const formData = await req.formData();
const from = formData.get("from");
// same for the other fields...
// make sure to do `typeof from === 'string'` and after that check if `from` is a valid string, at least that `from.trim()` is not empty.
if(typeof from !== 'string' || from.trim() === '') throw new Error("from field was invalid"); // something like that This means you don't need |
Beta Was this translation helpful? Give feedback.
-
Next.js SendGrid Integration Guide============================== This guide provides a comprehensive overview of implementing SendGrid integration with Next.js to process incoming email data. Table of ContentsPrerequisitesBefore implementing SendGrid integration, ensure you have: Required Dependencies
Environment VariablesCreate a
Implementation ApproachesThere are two main approaches to handling SendGrid POST requests in Next.js: 1. Route Handler (Recommended)// app/api/sendgrid/route.ts
import { neon } from "@neondatabase/serverless";
import { IncomingForm } from "formidable";
import { NextResponse } from "next/server";
export const config = {
api: {
bodyParser: false,
},
};
const parseForm = async (req) =>
new Promise((resolve, reject) => {
const form = new IncomingForm();
form.parse(req, (err, fields, files) => {
if (err) return reject(err);
resolve(fields);
});
});
export async function POST(request) {
try {
const fields = await parseForm(request);
const { from, to, subject, text, html } = fields;
const sql = neon(process.env.DATABASE_URL);
await sql`
INSERT INTO edgar_teams (
from_email,
to_email,
subject,
text_body,
html_body,
timestamp
)
VALUES (
${from},
${to},
${subject},
${text},
${html},
CURRENT_TIMESTAMP AT TIME ZONE 'America/Chicago'
)
`;
return NextResponse.json({ message: "Email received" }, { status: 200 });
} catch (error) {
console.error("Error processing email:", error);
return NextResponse.json(
{ error: "Failed to process email" },
{ status: 500 }
);
}
} 2. API Handler (Legacy Approach)// app/api/sendgrid.ts
import { neon } from "@neondatabase/serverless";
import { IncomingForm } from "formidable";
export const config = {
api: {
bodyParser: false,
},
};
const parseForm = async (req) =>
new Promise((resolve, reject) => {
const form = new IncomingForm();
form.parse(req, (err, fields, files) => {
if (err) return reject(err);
resolve(fields);
});
});
export default async function handler(req) {
if (req.method !== 'POST') {
return new Response('Method Not Allowed', { status: 405 });
}
try {
const fields = await parseForm(req);
const { from, to, subject, text, html } = fields;
const sql = neon(process.env.DATABASE_URL);
await sql`
INSERT INTO edgar_teams (
from_email,
to_email,
subject,
text_body,
html_body,
timestamp
)
VALUES (
${from},
${to},
${subject},
${text},
${html},
CURRENT_TIMESTAMP AT TIME ZONE 'America/Chicago'
)
`;
return new Response(JSON.stringify({ message: "Email received" }), {
headers: { 'Content-Type': 'application/json' },
status: 200
});
} catch (error) {
console.error("Error processing email:", error);
return new Response(
JSON.stringify({ error: "Failed to process email" }),
{ status: 500 }
);
}
} ConfigurationFile Structure
CORS SetupAdd to your module.exports = {
// Other configurations...
experimental: {
appDir: true
}
} TestingPostman Configuration
TroubleshootingCommon Issues
Debugging Tips
|
Beta Was this translation helpful? Give feedback.
Wait, so, you go to SendGrid, and you say, when an email is received, you can use this URL, and this URL is
/api/sendgrid
, right?Have you deployed this? is the latest version of your application, containing this change deployed?
One thing you have to fix is:
Change this:
to: