Skip to main content

Generate a Form with AI

Create a new form by providing a natural language prompt. The AI will generate appropriate form fields, labels, and validation rules based on your description.

Endpoint

Authentication

Requires Clerk authentication. The form will be associated with the authenticated user’s account.

Request Body

string
required
A natural language description of the form you want to create. Cannot be empty.Examples:
  • “Create a contact form with name, email, and message”
  • “Generate an event registration form with attendee details”
  • “Make a job application form with personal info and resume upload”

Response

boolean
Indicates whether the form was generated successfully
string
Unique identifier for the generated form (UUID format)
string
URL-friendly slug for accessing the form publicly
string
The generated title of the form
string
Confirmation message: “Form generated successfully”

Example Request

Example Response

Error Responses

Implementation Details

The endpoint performs the following steps:
  1. Validates the user’s Clerk authentication session
  2. Validates the prompt is not empty
  3. Calls the AI service to generate a form schema from the prompt
  4. Validates the generated schema structure
  5. Creates a new form record in the database with isPublished: false
  6. Returns the form ID, slug, and title
Source: app/api/forms/generate/route.ts:6

Next Steps

After generating a form:
  • Use the formId to fetch full form details via GET /api/forms/[id]
  • Use the slug to share a public URL: https://your-domain.com/forms/{slug}
  • Update the form’s publish status via PATCH /api/forms/[id]