Skip to main content

Submit Form Response

Submit a response to a published form. This endpoint validates that all required fields are present before saving the response.

Endpoint

POST /api/forms/submit

Authentication

No authentication required. This endpoint is public to allow anyone with the form link to submit responses.

Request Body

formId
string
required
The UUID of the form to submit a response to
data
object
required
An object containing the form field values. Keys should match the field IDs defined in the form schema.The structure varies based on the form, but generally:
  • Keys are field IDs (e.g., “name”, “email”, “rating”)
  • Values are the user’s input for each field

Response

success
boolean
Indicates whether the response was submitted successfully
message
string
Confirmation message: “Response submitted successfully”
responseId
string
Unique identifier for the submitted response

Validation Rules

The endpoint performs the following validations:
  1. Form ID validation: Must be a non-empty string
  2. Data validation: Must be a valid object (not null, not array)
  3. Form existence: The form must exist in the database
  4. Required fields: All fields marked as required: true in the form schema must be present in the data object
If any validation fails, the request is rejected with a 400 or 404 error.

Example Request

curl -X POST https://your-domain.com/api/forms/submit \
  -H "Content-Type: application/json" \
  -d '{
    "formId": "cm3x7y2z1a0b1c2d3e4f5",
    "data": {
      "name": "John Doe",
      "email": "john@example.com",
      "rating": 5,
      "comments": "Great service!"
    }
  }'

Example Response

{
  "success": true,
  "message": "Response submitted successfully",
  "responseId": "cm3z1a2b3c4d5e6f7g8h9"
}

Error Responses

Implementation Details

The submission process:
  1. Validates the formId and data parameters
  2. Looks up the form in the database
  3. Retrieves the form’s field schema
  4. Checks that all required fields are present in the submitted data
  5. Creates a new response record in the formsResponses table
  6. Returns the response ID
Source: app/api/forms/submit/route.ts:4

Example: Required Field Validation

Given a form with this field schema:
[
  { "id": "name", "label": "Your Name", "required": true },
  { "id": "email", "label": "Email", "required": true },
  { "id": "phone", "label": "Phone", "required": false }
]
Valid submission (phone is optional):
{
  "formId": "cm3x7y2z1a0b1c2d3e4f5",
  "data": {
    "name": "Jane Smith",
    "email": "jane@example.com"
  }
}
Invalid submission (missing required email):
{
  "formId": "cm3x7y2z1a0b1c2d3e4f5",
  "data": {
    "name": "Jane Smith"
  }
}
This would return:
{
  "error": "Required field missing: Email"
}