Download Log in

8. AI Generation API

Developer Guide

PRO endpoint for generating form fields from natural language descriptions. Requires AI credits; the base action cost is 3 credits before any oversized-token surcharge.

POST /formforge/v1/generate

Accepts a natural language prompt and returns an array of field definitions.

bash
curl -s -X POST -u "admin:XXXX XXXX XXXX XXXX" 
  -H "Content-Type: application/json" 
  https://example.com/wp-json/formforge/v1/generate 
  -d '{"prompt": "Create a job application form for a software developer position"}'
Response (200):
json
{
  "fields": [
    {"id": "field_1", "type": "text", "label": "Full Name", "required": true},
    {"id": "field_2", "type": "email", "label": "Email", "required": true},
    {"id": "field_3", "type": "phone", "label": "Phone", "required": false},
    {"id": "field_4", "type": "url", "label": "Portfolio URL", "required": false},
    {"id": "field_5", "type": "select", "label": "Years of Experience", "required": true, "options": [
      {"value": "0-2", "label": "0-2 years"},
      {"value": "3-5", "label": "3-5 years"},
      {"value": "5+", "label": "5+ years"}
    ]},
    {"id": "field_6", "type": "textarea", "label": "Cover Letter", "required": false, "rows": 6},
    {"id": "field_7", "type": "file_upload", "label": "Resume", "required": true, "accept": ".pdf,.doc,.docx"}
  ],
  "message": "Generated 7 fields."
}
Response (402 — no AI credits):
json
{
  "code": "no_credits",
  "message": "No AI credits remaining. Please upgrade your plan.",
  "data": { "status": 402 }
}

Pipeline: Generate then Create

bash
# Generate fields, then create a form
FIELDS=$(curl -s -X POST -u "admin:XXXX" 
  -H "Content-Type: application/json" 
  https://example.com/wp-json/formforge/v1/generate 
  -d '{"prompt": "Simple event RSVP form"}' | jq '.fields')

curl -s -X POST -u "admin:XXXX" 
  -H "Content-Type: application/json" 
  https://example.com/wp-json/formforge/v1/forms 
  -d "{"title": "Event RSVP", "fields": $FIELDS}"

Prompt Tips for Better Results

PromptResult Quality
"contact form"Basic (3-4 fields)
"Contact form with name, email, phone, subject dropdown, and message"Good (explicit fields)
"Job application for a senior developer with resume upload, cover letter, experience level, tech stack checkboxes, and salary expectation"Excellent (detailed, specific)

Generated field definitions are normalized before save. Current aliases include FILE/file/upload -> file_upload, PARAGRAPH/paragraph/html/content -> html_block, dropdown -> select, and tel -> phone. Unknown types are downgraded to text, so AI output cannot create unsupported frontend controls or stale notification payloads.

Admin-side Error Event (formforge:ai-error)

Whenever AI generation fails in the admin builder, Form Forge dispatches a jQuery event you can listen to from any other admin script (analytics, custom toast UIs, audit logs). The default UX — an inline error card with a friendly title, an explanation, and a context-aware action set (Retry, Buy credits, Open Dashboard, etc.) — runs first; the event lets extensions add behavior on top without re-implementing the classification logic.

js
jQuery( document ).on( 'formforge:ai-error', function( e, payload ) {
    // payload.code    — short user-facing title (e.g. "Кредиты закончились")
    // payload.message — full body text shown in the error card
    if ( window.myAnalytics ) {
        window.myAnalytics.track( 'formforge_ai_error', {
            code: payload.code,
            message: payload.message
        } );
    }
} );

The event fires for every classified case: 400 Invalid payload, 401/403 Unauthorized / pro_only, 402 / credits_exhausted, 429 Rate limit, 0 Network error, 5xx Server error, and the default fallback. The original alert() call was removed in 2026-04-28 — listeners replace it as the only side-channel for AI errors.

Forge AI Assistant Online

Hi! I'm the Form Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs