How-to guide
How to Handle Ad-Injection Errors in n8n
This guide shows how to handle ad-injection errors in a n8n app, the goal being to make sure the user still gets a reply if the ad service fails. It builds on the Monetzly server SDK, so the approach is specific to how n8n produces and streams responses.
Overview
Monetization should never break your n8n app. The Monetzly SDK is built for this: if the ad service connection fails or errors mid-stream, it falls back to yielding your original, un-injected token stream, so the user still gets a complete answer.
You should still wrap the injection loop in your normal error handling so a failure degrades to a plain n8n response rather than a broken request.
n8n apps are usually built for automated AI workflows, chatbot backends, and agent orchestration, so handle ad-injection errors typically comes up while a user is mid-conversation, the moment where monetization has to be additive rather than disruptive.
How this works on n8n
n8n runs on Node, so a Code node (or a small custom node) can import @monetzly/server-sdk and wrap the token output of an upstream LLM node. Because n8n items are usually discrete rather than a live token stream, confirm whether you inject over a buffered response or a true stream for your workflow.
Because Monetzly's inject() accepts any async token stream, the n8n side of this task is just mapping your output to it, no rewrite of your n8n model call.
Steps
- Wrap the injection loop in your standard n8n error handling.
- Rely on the SDK's automatic fallback to the original stream on ad-service failure.
- Log injection failures so you can monitor fill and uptime separately from your model.
Integration snippet
// n8n Code node (runOnceForEachItem). TODO_VERIFY: streaming vs buffered output.
import { MonetzlySDK } from "@monetzly/server-sdk";
const sdk = new MonetzlySDK({
apiKey: $env.MONETZLY_API_KEY,
serverAddress: $env.MONETZLY_SERVER_ADDRESS,
});
await sdk.connect();
const llmText = $json.text; // output from the previous LLM node
async function* asChunks() { yield { content: llmText }; }
let out = "";
for await (const t of sdk.inject(asChunks(), { prompt: $json.prompt })) out += t.content ?? "";
await sdk.disconnect();
return { json: { text: out } };Frequently asked questions
Start monetizing in about 5 minutes
Wrap your existing LLM response stream with the Monetzly SDK and earn on every session, no paywall required.