Menu

Generate form

POST/api/v1/environments/{environmentId}/forms/generate

Create a form from the API. You pass an existing form as a style template, then define the steps and components for the new one.

How it works

  1. You provide a source form ID. That form acts as a style template: the generated form inherits its fonts, colors, backgrounds, component styles, and layout settings.
  2. You define the steps and components for the new form. Each step contains one or more components (inputs, dropdowns, scheduling widgets, etc.).
  3. The API creates the form, publishes it, and returns the form ID and URL.

Request

cURL

TypeScript

curl -X POST 'https://forms.withsurface.com/api/v1/environments/{environmentId}/forms/generate' \
  -H 'x-api-key: your-api-key-here' \
  -H 'Content-Type: application/json' \
  -d '{
    "sourceFormId": "your-source-form-id",
    "name": "My Generated Form",
    "steps": [
      {
        "name": "Contact Info",
        "components": [
          {
            "type": "IdentityInfo",
            "content": {
              "fields": [
                { "key": "firstName", "label": "First Name" },
                { "key": "lastName", "label": "Last Name" },
                { "key": "emailAddress", "label": "Email" }
              ]
            }
          },
          {
            "type": "NextButton",
            "content": { "label": "Continue" }
          }
        ]
      },
      {
        "name": "Additional Details",
        "components": [
          {
            "type": "Dropdown",
            "content": {
              "question": "What is your role?",
              "options": ["Engineering", "Product", "Design", "Marketing", "Sales", "Other"]
            }
          },
          {
            "type": "NextButton",
            "content": { "label": "Submit" }
          }
        ]
      }
    ]
  }'
const response = await fetch(
  `https://forms.withsurface.com/api/v1/environments/${environmentId}/forms/generate`,
  {
    method: "POST",
    headers: {
      "x-api-key": "your-api-key-here",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      sourceFormId: "your-source-form-id",
      name: "My Generated Form",
      steps: [
        {
          name: "Contact Info",
          components: [
            {
              type: "IdentityInfo",
              content: {
                fields: [
                  { key: "firstName", label: "First Name" },
                  { key: "lastName", label: "Last Name" },
                  { key: "emailAddress", label: "Email" },
                ],
              },
            },
            {
              type: "NextButton",
              content: { label: "Continue" },
            },
          ],
        },
        {
          name: "Additional Details",
          components: [
            {
              type: "Dropdown",
              content: {
                question: "What is your role?",
                options: ["Engineering", "Product", "Design", "Marketing", "Sales", "Other"],
              },
            },
            {
              type: "NextButton",
              content: { label: "Submit" },
            },
          ],
        },
      ],
    }),
  }
);

const data = await response.json();
console.log(data.result.form); // { id: "...", url: "/s/..." }

Body parameters

string

The ID of an existing form to use as a style template. The generated form inherits all visual settings (fonts, colors, backgrounds, component styles) from this form.

string

Display name for the generated form.

array

Array of step objects (minimum 2). Each step represents a page/screen in the form.

Step object properties

string

Display name for the step.

array

Array of component objects (minimum 1). Each component is a form element within the step.

Component object properties

string

The component type. See Supported Component Types below.

object

Component-specific content. The shape depends on the component type.

Supported component types

Input components

component

Single-line text input.

Content fields
FieldTypeDescription
questionstringThe question/label text

component

Multi-line text area.

Content fields
FieldTypeDescription
questionstringThe question/label text

component

Collects identity fields (name, email, company, etc.). This component has a strict schema: only the fields array is accepted.

Content fields
FieldTypeRequiredDescription
fieldsarrayYesArray of field objects
fields[].keystringYesField identifier. Valid keys: "firstName", "lastName", "fullName", "emailAddress", "workEmailAddress", "phoneNumber", "companyName", "numberOfEmployees", "websiteUrl", "title", "streetAddress", "aptSuiteEtc", "city", "state", "country", "zipCode", "companyRevenue", "profilePictureUrl"
fields[].labelstringYesDisplay label
fields[].placeholderstringNoField hint text
fields[].includeDialCodebooleanNoInclude dial code selector (for phone fields)
fields[].defaultDialCodeCountrystringNoDefault country for dial code
fields[].blockPersonalEmailsbooleanNoBlock personal email domains (for email fields)
fields[].revealAfterstringNoKey of another field that must be filled before this one is shown

Selection components

component

Dropdown select menu. Requires at least one option.

Content fields
FieldTypeRequiredDescription
questionstringNoThe question/label text
optionsstring[]YesArray of dropdown options (at least 1). You can also use choices as an alias.
placeholderstringNoField hint text
isMultibooleanNoAllow multiple selections
hideQuestionbooleanNoHide the question label

component

Single or multi-select options question.

Content fields
FieldTypeDescription
questionstringThe question/label text
choicesstring[]Array of choice options. You can also use options as an alias.
subheadingstringSubheading text below the question
isMultibooleanAllow multiple selections

Scheduling components

component

Embeds a calendar scheduling widget (supports Calendly, Cal.com, HubSpot, SavvyCal, RevenueHero, and more). Requires a calendar URL.

Content fields
FieldTypeRequiredDescription
dataUrlstringYesThe calendar scheduling URL. You can also use url or calendarUrl as aliases.
messagestringNoMessage displayed above the calendar
calendarTypestringNoCalendar provider: "Calendly", "Cal", "Hubspot", "SavvyCal", "RevenueHero", "ReclaimAI", "Clari", "ChiliCal"
prefillEmailbooleanNoAuto-fill email from earlier form steps

component

Button to advance to the next step.

Content fields
FieldTypeDescription
labelstringButton text (alias for buttonText)

Display components

component

A text header displayed at the top of a step.

Content fields
FieldTypeDescription
titlestringThe heading text
subheadingstringOptional subheading text below the title

component

A block of disclaimer or legal text.

Content fields
FieldTypeDescription
textstringThe disclaimer text content

Other components

Additional component types are supported: EmailForm, FileUploader, GraphicOptions, PasswordInput, and more. These follow the same pattern: pass the type and relevant content fields.

Response

success boolean

Whether the form was created successfully.

result object

Result object properties

form object

Form object properties

id string

Unique identifier for the generated form.

url string

The public URL path for the form (e.g., /s/{formId}).

Example Response

{
  "success": true,
  "result": {
    "form": {
      "id": "cm5abc123def456",
      "url": "/s/cm5abc123def456"
    }
  }
}

Error Responses

400 - Invalid Request

404 - Source Form Not Found

{
  "message": "Invalid request body",
  "errors": [
    {
      "message": "CalendlyScreen requires a calendar URL (via 'dataUrl', 'url', or 'calendarUrl')",
      "path": ["steps", 0, "components", 1, "content"]
    }
  ]
}
{
  "message": "Source form not found"
}

Form with scheduling

cURL

TypeScript

curl -X POST 'https://forms.withsurface.com/api/v1/environments/{environmentId}/forms/generate' \
  -H 'x-api-key: your-api-key-here' \
  -H 'Content-Type: application/json' \
  -d '{
    "sourceFormId": "your-source-form-id",
    "name": "Demo Booking Form",
    "steps": [
      {
        "name": "Your Info",
        "components": [
          {
            "type": "IdentityInfo",
            "content": {
              "fields": [
                { "key": "firstName", "label": "First Name" },
                { "key": "lastName", "label": "Last Name" },
                { "key": "emailAddress", "label": "Work Email" },
                { "key": "companyName", "label": "Company" }
              ]
            }
          },
          {
            "type": "NextButton",
            "content": { "label": "Book a Demo" }
          }
        ]
      },
      {
        "name": "Schedule",
        "components": [
          {
            "type": "CalendlyScreen",
            "content": {
              "dataUrl": "https://calendly.com/your-team/30min",
              "message": "Pick a time that works for you"
            }
          }
        ]
      }
    ]
  }'
const response = await fetch(
  `https://forms.withsurface.com/api/v1/environments/${environmentId}/forms/generate`,
  {
    method: "POST",
    headers: {
      "x-api-key": "your-api-key-here",
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      sourceFormId: "your-source-form-id",
      name: "Demo Booking Form",
      steps: [
        {
          name: "Your Info",
          components: [
            {
              type: "IdentityInfo",
              content: {
                fields: [
                  { key: "firstName", label: "First Name" },
                  { key: "lastName", label: "Last Name" },
                  { key: "emailAddress", label: "Work Email" },
                  { key: "companyName", label: "Company" },
                ],
              },
            },
            { type: "NextButton", content: { label: "Book a Demo" } },
          ],
        },
        {
          name: "Schedule",
          components: [
            {
              type: "CalendlyScreen",
              content: {
                dataUrl: "https://calendly.com/your-team/30min",
                message: "Pick a time that works for you",
              },
            },
          ],
        },
      ],
    }),
  }
);