Skip to main content
← All notes
Building

The contact API formats unknown form fields into email text by converting snake case keys to title case

software

The formatServiceFields function in api/contact/route.ts does not know what fields each service sends. It receives the entire request body as a Record of string to unknown, creates a skip set containing name, email, message, service, and website, then iterates every remaining key-value pair. Each key gets two regex replacements — underscores become spaces, then the first letter of each word capitalises via a word-boundary match. The value prints as-is. The message field gets special treatment — it appends last with a blank line separator so the freeform text always appears at the bottom. This means the audio intake form can send track_count and genre, the software form can send budget_range and project_type, and the musician form can send instruments_needed and session_length, and all of them arrive in the email as Track Count, Budget Range, Instruments Needed — readable labels generated from the form field names themselves. Adding a new field to a service intake form in ServiceSidebar requires zero changes to the API. The FieldConfig array in ServiceSidebar defines the field name, label, and type. The ServiceIntakeForm component renders it. The form posts every field. The API formats whatever it receives. Four files in the chain and only one needs editing. The function accepts a service parameter that it never uses — the underscore prefix on _service marks it as intentionally ignored. The service type drives the email subject line via a separate SERVICE_LABELS lookup, but the field formatting is genuinely generic. One function, two regex calls, any number of fields, and the email reads like a human wrote it.

Comments coming soon

Sign in with TikTok to leave a comment. Coming soon.