Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

chore(server): post the other webhooks with postJson, as Send to CMS does (#1437 review) (#1568)

Cihand3e05711ad72
diff --git a/docs/api-reference/webhooks.mdx b/docs/api-reference/webhooks.mdx
index 68c00645e..7b70e5d5d 100644
--- a/docs/api-reference/webhooks.mdx
+++ b/docs/api-reference/webhooks.mdx
@@ -110,7 +110,7 @@ Payloads are flat JSON objects with an `event` field at the top level.
- `brief` is `null` until a content brief has been generated for the opportunity.
- `callback_url` points at `POST /webhooks/content/{id}/status`; `callback_method` is `POST`. Send `{ "status": "in_progress" }` (or `new`, `reviewed`, `sent`, `done`, `dismissed`) as JSON and pass the same secret configured for this active webhook in the `X-Webhook-Secret` header. Configure a non-empty webhook secret in **Content → Configure Webhook** before using callbacks; callback authentication fails with 401 when no secret is configured. Callbacks are limited to 1,200 requests per 15 minutes per secret hash, with a separate per-IP limiter for failed (401) authentication attempts (10 per 15 minutes). They do not require a dashboard JWT.
-When your endpoint responds, the opportunity is marked `sent` and the response status plus the first 1,000 characters of its body are stored only if the opportunity's status and update timestamp have not changed since delivery began. If your workflow calls `callback_url` before replying (or a user updates the opportunity during delivery), the newer status is preserved and the late response does not overwrite it. If the request times out or the connection fails, the opportunity keeps its previous status.
+When your endpoint responds, with any status (a redirect too), the opportunity is marked `sent` and the response status plus the first 1,000 characters of its body are stored only if the opportunity's status and update timestamp have not changed since delivery began. If your workflow calls `callback_url` before replying (or a user updates the opportunity during delivery), the newer status is preserved and the late response does not overwrite it. If the request times out or the connection fails, the opportunity keeps its previous status.
### `daily_pulse.created`
@@ -293,11 +293,12 @@ The test uses the URL and secret currently in the form, so you can check an endp
- Each event is sent **once**. There are no automatic retries.
- Ansvisor waits up to 15 seconds for a response (10 seconds for `webhook.test`).
- Respond with a 2xx status quickly and do slow work after responding.
+- Redirects aren't followed: a 3xx is the answer, and nothing reaches the address it points to. Use that address; the **Test** button reports a 3xx as a failure.
+- On Ansvisor Cloud the webhook must be a public address. A self-hosted install may also use addresses on its own network.
- `content_draft.sent`:
- - A 2xx answer marks the send **sent**. Anything else, or no answer, marks it **failed**, and the Library shows why. Redirects aren't followed, and the first 2,000 characters of the answer are kept.
+ - A 2xx answer marks the send **sent**. Anything else, or no answer, marks it **failed**, and the Library shows why. The first 2,000 characters of the answer are kept.
- A single page is sent while the person waits. A bulk send (up to 25 pages) goes on in the background, one page after another, with a 2-second pause between pages.
- A workspace can send 200 pages in 24 hours.
- - On Ansvisor Cloud the webhook must be a public address. A self-hosted install may also use addresses on its own network.
<Card title="Help center" icon="circle-question" href="/faq">
Common questions and troubleshooting.
diff --git a/server/src/lib/webhook-dispatch.js b/server/src/lib/webhook-dispatch.js
index f8961e08c..9578b72f8 100644
--- a/server/src/lib/webhook-dispatch.js
+++ b/server/src/lib/webhook-dispatch.js
@@ -1,13 +1,15 @@
import supabaseAdmin from '../config/supabase.js';
import { logger } from './logger.js';
+import { postJson } from './safe-post.js';
/**
* Deliver an event payload to a brand's active webhook configs.
*
* Same wire format as the Content Optimization send routes: a flat JSON
* payload with an `event` field, plus the shared secret echoed in the
- * `X-Webhook-Secret` header when one is configured. Delivery is
- * best-effort — failures are logged, never thrown.
+ * `X-Webhook-Secret` header when one is configured. Posted as Send to CMS
+ * posts (postJson): no private address on Cloud, no redirect followed, 15 s.
+ * Delivery is best-effort — failures are logged, never thrown.
*
* Returns { sent, failed } delivery counts.
*/
@@ -23,21 +25,17 @@ export async function dispatchBrandWebhook(brandId, payload) {
let sent = 0;
let failed = 0;
+ const body = JSON.stringify(payload);
for (const config of configs) {
- const headers = { 'Content-Type': 'application/json' };
+ const headers = {};
if (config.webhook_secret) {
headers['X-Webhook-Secret'] = config.webhook_secret;
}
try {
- const res = await fetch(config.webhook_url, {
- method: 'POST',
- headers,
- body: JSON.stringify(payload),
- signal: AbortSignal.timeout(15000),
- });
- if (res.ok) {
+ const res = await postJson(config.webhook_url, body, { headers });
+ if (res.status >= 200 && res.status < 300) {
sent++;
} else {
failed++;
diff --git a/server/src/lib/webhook-dispatch.test.js b/server/src/lib/webhook-dispatch.test.js
index d1fe3b909..1c1571a58 100644
--- a/server/src/lib/webhook-dispatch.test.js
+++ b/server/src/lib/webhook-dispatch.test.js
@@ -1,14 +1,15 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
-const { mockFrom, mockFetch, state } = vi.hoisted(() => ({
+const { mockFrom, postJson, state } = vi.hoisted(() => ({
mockFrom: vi.fn(),
- mockFetch: vi.fn(),
+ postJson: vi.fn(),
state: { configs: [] },
}));
vi.mock('../config/supabase.js', () => ({
default: { from: mockFrom },
}));
+vi.mock('./safe-post.js', () => ({ postJson: (...args) => postJson(...args) }));
vi.mock('./logger.js', () => ({
logger: { warn: vi.fn(), error: vi.fn(), info: vi.fn() },
}));
@@ -46,9 +47,8 @@ beforeEach(() => {
expect(table).toBe('webhook_configs');
return queryForConfigs();
});
- mockFetch.mockReset();
- mockFetch.mockResolvedValue({ ok: true, status: 204 });
- vi.stubGlobal('fetch', mockFetch);
+ postJson.mockReset();
+ postJson.mockResolvedValue({ status: 204, answer: '' });
});
describe('dispatchBrandWebhook', () => {
@@ -94,19 +94,30 @@ describe('dispatchBrandWebhook', () => {
});
expect(result).toEqual({ sent: 1, failed: 0 });
- expect(mockFetch).toHaveBeenCalledTimes(1);
- expect(mockFetch).toHaveBeenCalledWith(
+ // Posted as Send to CMS posts: the safe POST sets the content type.
+ expect(postJson).toHaveBeenCalledTimes(1);
+ expect(postJson).toHaveBeenCalledWith(
'https://pulse.example/hook',
- expect.objectContaining({
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-Webhook-Secret': 'pulse-secret',
- },
- }),
+ JSON.stringify({ event: 'daily_pulse.created', pulse: { date: '2026-10-20' } }),
+ { headers: { 'X-Webhook-Secret': 'pulse-secret' } },
);
});
+ it('sends no secret header to a webhook without one', async () => {
+ state.configs = [
+ {
+ id: 'open',
+ brand_id: 'brand-1',
+ webhook_url: 'https://open.example/hook',
+ webhook_secret: null,
+ is_active: true,
+ events: ['opportunity.sent'],
+ },
+ ];
+ await dispatchBrandWebhook('brand-1', { event: 'opportunity.sent' });
+ expect(postJson.mock.calls[0][2]).toEqual({ headers: {} });
+ });
+
it('counts delivery failures without throwing', async () => {
state.configs = [
{
@@ -126,8 +137,9 @@ describe('dispatchBrandWebhook', () => {
events: ['opportunity.sent'],
},
];
- mockFetch
- .mockResolvedValueOnce({ ok: false, status: 503 })
+ // A redirect is an answer, as a 503 is: none is followed.
+ postJson
+ .mockResolvedValueOnce({ status: 302, answer: '' })
.mockRejectedValueOnce(new Error('connection refused'));
await expect(dispatchBrandWebhook('brand-1', { event: 'opportunity.sent' })).resolves.toEqual({
@@ -151,6 +163,6 @@ describe('dispatchBrandWebhook', () => {
await expect(
dispatchBrandWebhook('brand-1', { event: 'daily_pulse.created' }),
).resolves.toEqual({ sent: 0, failed: 0 });
- expect(mockFetch).not.toHaveBeenCalled();
+ expect(postJson).not.toHaveBeenCalled();
});
});
diff --git a/server/src/routes/content-webhooks.test.js b/server/src/routes/content-webhooks.test.js
new file mode 100644
index 000000000..f0810b8c3
--- /dev/null
+++ b/server/src/routes/content-webhooks.test.js
@@ -0,0 +1,242 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import express from 'express';
+
+/**
+ * Route-level tests for the Opportunities webhook sends (POST
+ * /api/content/:id/send-webhook and /bulk/send-webhook) and the webhook test
+ * (POST /api/content/webhook-test): they post as Send to CMS does
+ * (postJson), so on Cloud a private address is refused and a redirect is an
+ * answer, not followed (#1437 review, item 5).
+ */
+
+import { PageFetchError } from '../lib/safe-fetch.js';
+
+const postJson = vi.fn();
+const assertOpportunityAccess = vi.fn();
+let rows = {};
+let updates = [];
+
+let server;
+let baseUrl;
+
+/** A query on `table`: every filter returns it, and it reads as the table's rows. */
+function query(table) {
+ const answer = () => ({ data: rows[table] ?? null, error: null });
+ const chain = new Proxy(
+ {},
+ {
+ get: (_t, op) => {
+ if (op === 'then')
+ return (resolve, reject) => Promise.resolve(answer()).then(resolve, reject);
+ if (op === 'maybeSingle' || op === 'single') {
+ return async () => {
+ const data = rows[table] ?? null;
+ return { data: Array.isArray(data) ? (data[0] ?? null) : data, error: null };
+ };
+ }
+ if (op === 'update') {
+ return (values) => {
+ updates.push({ table, values });
+ return chain;
+ };
+ }
+ return () => chain;
+ },
+ },
+ );
+ return chain;
+}
+
+async function startApp() {
+ vi.resetModules();
+ vi.doMock('../lib/safe-post.js', () => ({ postJson: (...args) => postJson(...args) }));
+ // As the other content route tests: the jobs and workers aren't loaded.
+ vi.doMock('../lib/plan-guard.js', () => ({
+ PlanLimitError: class PlanLimitError extends Error {},
+ requireFeature: () => (req, res, next) => next(),
+ enforceBriefQuota: vi.fn(),
+ getBriefQuotaStatus: vi.fn(),
+ getContentUsage: vi.fn(),
+ getOpportunityRequestAllowance: vi.fn(),
+ getOrgIdForUser: vi.fn(),
+ }));
+ vi.doMock('../lib/job-manager.js', () => ({ createJob: vi.fn(), getJob: vi.fn() }));
+ vi.doMock('../lib/job-runner.js', () => ({ runContentJob: vi.fn() }));
+ vi.doMock('../lib/access.js', () => ({
+ assertBrandAccess: vi.fn(),
+ assertOpportunitiesAccess: vi.fn(),
+ assertOpportunityAccess,
+ assertPromptAccess: vi.fn(),
+ }));
+ vi.doMock('../config/supabase.js', () => ({ default: { from: (table) => query(table) } }));
+ vi.doMock('../lib/prompt-clusters.js', () => ({ loadClusters: vi.fn() }));
+ vi.doMock('../lib/action-center/from-opportunity.js', () => ({
+ OPPORTUNITY_ACTION_KIND: 'content_opportunity',
+ sendToActionCenter: vi.fn(),
+ }));
+ vi.doMock('../lib/ai-provider.js', () => ({ resolveModel: vi.fn() }));
+ vi.doMock('../lib/logger.js', () => {
+ const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), fatal: vi.fn() };
+ return { default: logger, logger };
+ });
+
+ const { default: router } = await import('./content.js');
+ const app = express();
+ app.use(express.json());
+ app.use((req, _res, next) => {
+ req.user = { id: 'u1' };
+ req.log = { error: vi.fn(), info: vi.fn() };
+ next();
+ });
+ app.use('/api/content', router);
+
+ await new Promise((resolve) => {
+ server = app.listen(0, resolve);
+ });
+ baseUrl = `http://127.0.0.1:${server.address().port}/api/content`;
+}
+
+const post = (path, body = {}) =>
+ fetch(`${baseUrl}${path}`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify(body),
+ });
+
+const OPPORTUNITY = {
+ id: 'o1',
+ brand_id: 'b1',
+ prompt_id: null,
+ title: 'Pricing comparison',
+ description: 'Compare the plans.',
+ type: 'comparison',
+ impact: 'high',
+ opportunity_score: '0.8',
+ source_data: {},
+ brief: null,
+ status: 'new',
+ updated_at: '2026-10-27T08:00:00Z',
+};
+
+beforeEach(async () => {
+ vi.clearAllMocks();
+ updates = [];
+ rows = {
+ webhook_configs: {
+ id: 'w1',
+ webhook_url: 'https://hooks.example/abc',
+ webhook_secret: 'shh',
+ events: ['opportunity.sent'],
+ },
+ brands: { name: 'Acme', industry: 'SaaS' },
+ brand_domains: [{ domain: 'acme.example' }],
+ competitors: [],
+ content_opportunities: { status: 'sent' },
+ };
+ assertOpportunityAccess.mockResolvedValue(OPPORTUNITY);
+ postJson.mockResolvedValue({ status: 200, answer: '{"ok":true}' });
+ await startApp();
+});
+
+afterEach(async () => {
+ await new Promise((resolve) => server.close(resolve));
+});
+
+describe('POST /api/content/webhook-test', () => {
+ it('posts the test event with the secret, for 10 s, and says how it was answered', async () => {
+ const res = await post('/webhook-test', {
+ webhookUrl: 'https://hooks.example/abc',
+ webhookSecret: 'shh',
+ });
+ expect(await res.json()).toEqual({ success: true, status: 200, statusText: 'OK' });
+ const [address, body, opts] = postJson.mock.calls[0];
+ expect(address).toBe('https://hooks.example/abc');
+ expect(JSON.parse(body)).toMatchObject({ event: 'webhook.test' });
+ expect(opts).toEqual({ headers: { 'X-Webhook-Secret': 'shh' }, timeoutMs: 10_000 });
+ });
+
+ it('reads a redirect as an answer, not a success', async () => {
+ postJson.mockResolvedValue({ status: 302, answer: '' });
+ const res = await post('/webhook-test', { webhookUrl: 'https://hooks.example/abc' });
+ expect(await res.json()).toEqual({ success: false, status: 302, statusText: 'Found' });
+ expect(postJson.mock.calls[0][2]).toEqual({ headers: {}, timeoutMs: 10_000 });
+ });
+
+ it('says why an address it refused or couldn’t reach failed', async () => {
+ postJson.mockRejectedValue(new PageFetchError('blocked', '10.0.0.5 is not a public address'));
+ const res = await post('/webhook-test', { webhookUrl: 'http://10.0.0.5/hook' });
+ expect(await res.json()).toEqual({
+ success: false,
+ status: 0,
+ error: '10.0.0.5 is not a public address',
+ });
+ });
+});
+
+describe('POST /api/content/:id/send-webhook', () => {
+ it('posts the opportunity to the brand’s webhook with its secret, and keeps the answer', async () => {
+ const res = await post('/o1/send-webhook');
+ expect(res.status).toBe(200);
+ expect(await res.json()).toEqual({
+ success: true,
+ webhookStatus: 200,
+ opportunityStatus: 'sent',
+ });
+ const [address, body, opts] = postJson.mock.calls[0];
+ expect(address).toBe('https://hooks.example/abc');
+ expect(JSON.parse(body)).toMatchObject({
+ event: 'opportunity.sent',
+ opportunity: { id: 'o1', title: 'Pricing comparison' },
+ brand: { name: 'Acme', domain: 'acme.example' },
+ });
+ expect(opts).toEqual({ headers: { 'X-Webhook-Secret': 'shh' } });
+ const write = updates.find((u) => u.table === 'content_opportunities');
+ expect(write.values).toMatchObject({
+ status: 'sent',
+ webhook_response: { status: 200, body: '{"ok":true}' },
+ });
+ });
+
+ it('marks it sent on a redirect, as on any answer, keeping the status it got', async () => {
+ postJson.mockResolvedValue({ status: 302, answer: '' });
+ const res = await post('/o1/send-webhook');
+ expect(await res.json()).toMatchObject({ success: true, webhookStatus: 302 });
+ const write = updates.find((u) => u.table === 'content_opportunities');
+ expect(write.values).toMatchObject({ status: 'sent', webhook_response: { status: 302 } });
+ });
+
+ it('answers 500 when the address is refused, and marks nothing sent', async () => {
+ postJson.mockRejectedValue(new PageFetchError('blocked', '10.0.0.5 is not a public address'));
+ const res = await post('/o1/send-webhook');
+ expect(res.status).toBe(500);
+ expect(await res.json()).toMatchObject({
+ error: 'Failed to send webhook',
+ details: '10.0.0.5 is not a public address',
+ });
+ expect(updates.find((u) => u.table === 'content_opportunities')).toBeUndefined();
+ });
+});
+
+describe('POST /api/content/bulk/send-webhook', () => {
+ it('posts each opportunity as the single send does, and counts a refused one failed', async () => {
+ rows.content_opportunities = [OPPORTUNITY, { ...OPPORTUNITY, id: 'o2', title: 'FAQ page' }];
+ postJson
+ .mockResolvedValueOnce({ status: 200, answer: 'ok' })
+ .mockRejectedValueOnce(new Error('The webhook could not be reached'));
+ const res = await post('/bulk/send-webhook', { ids: ['o1', 'o2'] });
+ expect(await res.json()).toEqual({ sent: 1, failed: 1 });
+ expect(
+ postJson.mock.calls.map(([address, body, opts]) => [
+ address,
+ JSON.parse(body).opportunity.id,
+ opts,
+ ]),
+ ).toEqual([
+ ['https://hooks.example/abc', 'o1', { headers: { 'X-Webhook-Secret': 'shh' } }],
+ ['https://hooks.example/abc', 'o2', { headers: { 'X-Webhook-Secret': 'shh' } }],
+ ]);
+ const writes = updates.filter((u) => u.table === 'content_opportunities');
+ expect(writes).toHaveLength(1);
+ expect(writes[0].values.webhook_response).toEqual({ status: 200, body: 'ok' });
+ });
+});
diff --git a/server/src/routes/content.js b/server/src/routes/content.js
index 9ee28ff6d..6472e427c 100644
--- a/server/src/routes/content.js
+++ b/server/src/routes/content.js
@@ -1,3 +1,4 @@
+import { STATUS_CODES } from 'node:http';
import { Router } from 'express';
import { generateObject } from 'ai';
import { z } from 'zod';
@@ -16,6 +17,7 @@ import { resolveModel } from '../lib/ai-provider.js';
import { getLanguageName } from '../lib/languages.js';
import supabaseAdmin from '../config/supabase.js';
import { selectInChunks } from '../lib/chunked-in.js';
+import { postJson } from '../lib/safe-post.js';
import {
assertBrandAccess,
assertOpportunitiesAccess,
@@ -885,7 +887,8 @@ router.post('/bulk/send-webhook', async (req, res) => {
const serverUrl =
process.env.PUBLIC_SERVER_URL || `http://localhost:${process.env.PORT || 3001}`;
- const headers = { 'Content-Type': 'application/json' };
+ // The content type is postJson's own.
+ const headers = {};
if (webhookConfig.webhook_secret) {
headers['X-Webhook-Secret'] = webhookConfig.webhook_secret;
}
@@ -930,14 +933,11 @@ router.post('/bulk/send-webhook', async (req, res) => {
callback_method: 'POST',
};
- const webhookRes = await fetch(webhookConfig.webhook_url, {
- method: 'POST',
+ // As Send to CMS posts: no private address on Cloud, no redirect followed, 15 s.
+ const webhookRes = await postJson(webhookConfig.webhook_url, JSON.stringify(payload), {
headers,
- body: JSON.stringify(payload),
- signal: AbortSignal.timeout(15000),
});
-
- const responseBody = await webhookRes.text().catch(() => '');
+ const responseBody = webhookRes.answer;
// A workflow may call callback_url before it sends its HTTP response.
// Guard the write with the state observed before POST so this delayed
@@ -1112,19 +1112,16 @@ router.post('/:id/send-webhook', async (req, res) => {
callback_method: 'POST',
};
- const headers = { 'Content-Type': 'application/json' };
+ const headers = {};
if (webhookConfig.webhook_secret) {
headers['X-Webhook-Secret'] = webhookConfig.webhook_secret;
}
- const webhookRes = await fetch(webhookConfig.webhook_url, {
- method: 'POST',
+ // As the bulk send above posts.
+ const webhookRes = await postJson(webhookConfig.webhook_url, JSON.stringify(payload), {
headers,
- body: JSON.stringify(payload),
- signal: AbortSignal.timeout(15000),
});
-
- const responseBody = await webhookRes.text().catch(() => '');
+ const responseBody = webhookRes.answer;
const { data: statusUpdate } = await supabaseAdmin
.from('content_opportunities')
@@ -1188,22 +1185,22 @@ router.post('/webhook-test', async (req, res) => {
timestamp: new Date().toISOString(),
};
- const headers = { 'Content-Type': 'application/json' };
+ const headers = {};
if (webhookSecret) {
headers['X-Webhook-Secret'] = webhookSecret;
}
- const webhookRes = await fetch(webhookUrl, {
- method: 'POST',
+ // Posted as a real send is, so the test says what a send would meet:
+ // on Cloud a private address is refused, and a redirect is an answer.
+ const webhookRes = await postJson(webhookUrl, JSON.stringify(payload), {
headers,
- body: JSON.stringify(payload),
- signal: AbortSignal.timeout(10000),
+ timeoutMs: 10_000,
});
return res.json({
- success: webhookRes.ok,
+ success: webhookRes.status >= 200 && webhookRes.status < 300,
status: webhookRes.status,
- statusText: webhookRes.statusText,
+ statusText: STATUS_CODES[webhookRes.status] ?? '',
});
} catch (error) {
return res.json({