Spam doesn’t care what your site runs on. A contact form on a Next.js app or a comment box on a Cloudflare Worker gets the same junk a WordPress blog does. After the PHP SDK in June, we’re excited to release the official JavaScript SDK for Akismet.
@automattic/akismet is a TypeScript client for the Akismet API. It’s for developers adding spam protection to comments, contact forms, or signups in a JavaScript backend.
node:http.npm install @automattic/akismet
Then check each submission in the route that receives it. In a Next.js app, that’s app/api/comments/route.ts:
import {
Akismet, AkismetError, ContentType, SpamVerdict,
} from '@automattic/akismet';
const akismet = new Akismet({
apiKey: process.env.AKISMET_API_KEY!,
site: 'https://your-site.com',
isTest: process.env.AKISMET_TEST_MODE === '1', // Disable learning
});
export async function POST(request: Request) {
const { comment, name, email } = await request.json();
try {
const result = await akismet
.forRequest(request, { trustedProxyHops: 1 })
.check({
body: comment,
authorName: name,
authorEmail: email,
type: ContentType.Comment,
});
console.log(result.verdict);
if (result.verdict === SpamVerdict.Discard) {
// Akismet is sure it's spam. Drop it.
} else if (result.isSpam) {
// Hold it for moderation.
} else {
// Publish it.
}
} catch (error) {
if (!(error instanceof AkismetError)) throw error;
// Safe to log: the SDK redacts the API key.
console.error(error);
// Akismet failed or rejected the input. Hold it for review.
}
return new Response(null, { status: 202 });
}
site is the front page of your site. Check out the README for details on how the the Client IP is configured.
Every framework follows the same flow: check the submission, then act on the verdict. You can check out some examples for Express, Fastify, Hono, node:http, and Next.js Server Actions.
The Akismet SDK is open source under the MIT license.
SKILL.md that ships with @automattic/akismet.You’ll need an Akismet API key. The Personal plan is name-your-price for personal sites and blogs, and commercial sites need a paid plan sized to their traffic. Your code stays the same whichever plan you’re on.
Found a bug, or something that doesn’t fit your stack? Open an issue on GitHub.
Josh Allen's wife, Hailee Steinfeld, revealed an emotional dream she has about her daughter.
Clyde and Verna Nelson's shared Brooklyn home has been at the center of a complicated…