DocumentationAI generation API

AI generation API

The /ai/generate, /ai/generate-template and /ai/improve endpoints, the 402 hard block and the 20/min limit.

Reference for the AI assistant endpoints. All of them run under JwtAuthGuard + TenantGuard, and the three generation endpoints are limited to 20 requests/min per user.

Shared context

The three generation endpoints accept these optional context fields, on top of their own:

FieldNotes
templateId / versionIdAnchors the result to a template/version
localeTarget language (up to 10 characters)
templateDescriptionPurpose description (up to 500)
currentSubjectCurrent subject as a reference (up to 200)
variablesList of template variables

POST /ai/generate

Generates free text. Responds 200.

FieldRequiredNotes
promptYesInstruction (up to 4000 characters)
taskNosubjects, plain_text or general
systemPromptNoOverrides the system prompt
modelNoForces a specific model

Response: { content, model, tokens: { prompt, completion } }.

POST /ai/generate-template

Builds a full template. Responds 200.

FieldRequiredNotes
promptYesThe email you want (up to 4000)
toneNoDesired tone (up to 120)
modelNoForces a specific model

Response: { subject, preheader, html, model, tokens }.

POST /ai/improve

Rewrites existing HTML. Responds 200.

FieldRequiredNotes
htmlYesHTML to improve (up to 50000 characters)
instructionYesWhat to change (up to 500)
modelNoForces a specific model

Response: { content, model, tokens }.

POST /ai/improve
Authorization: Bearer <accessToken>
X-Tenant-Id: {tenantId}
Content-Type: application/json

{
  "html": "<h1>Hi</h1><p>...</p>",
  "instruction": "Make it shorter with a clear CTA"
}

Token hard block (402)

Before calling the provider, the API estimates the cost (ceil((prompt length) / 4)) and checks your AI token balance. If the balance does not cover the estimate, it responds 402 AI_TOKEN_QUOTA_EXCEEDED and consumes nothing — there is no grace period, unlike the email quota.

Rate limit

The three generation endpoints are limited to 20 requests per minute per user (counted by user:<id>). Exceeding it returns 429. This limit tightens the global limit to protect an expensive resource.

Balance & history

MethodPathDescription
GET/ai/balanceCurrent token balance ({ balance })
GET/ai/usagePaginated usage history (?page=&limit=)

GET /ai/balance and GET /ai/usage are not subject to the 20/min limit (only the global one).