Falkenstein, Germany · Hetzner Online GmbH
FormHaven

An Angular contact form for a static build

One standalone component and provideHttpClient() are all it takes. The component posts the form as JSON and shows each error under its field, and every message is spam filtered, stored in the EU and emailed to you. Your ng build output stays a folder of static files.

14 days free, no card. Paste the address where the code says YOUR_FORM_ID.

src/app/contact-form.ts
import { Component, inject, signal } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';

type Problem = { code: string; field?: string; message: string };
type Reply = { ok: boolean; message?: string; errors?: Problem[] };

@Component({
  selector: 'app-contact-form',
  template: `
    <form action="https://formhaven.net/f/YOUR_FORM_ID" method="POST" (submit)="send($event)">
      <label>Your email <input type="email" name="email" required></label>
      @if (problem('email'); as text) { <p>{{ text }}</p> }
      <label>Message <textarea name="message" required></textarea></label>
      <input type="text" name="_gotcha" style="display:none" tabindex="-1" autocomplete="off">
      <button type="submit" [disabled]="sending()">{{ sending() ? 'Sending...' : 'Send' }}</button>
      <p role="status">{{ status() }}</p>
    </form>
  `,
})
export class ContactForm {
  private http = inject(HttpClient);
  protected sending = signal(false);
  protected status = signal('');
  private problems = signal<Problem[]>([]);

  protected problem(field: string) {
    return this.problems().find((p) => p.field === field)?.message;
  }

  protected send(event: SubmitEvent) {
    event.preventDefault();
    const form = event.target as HTMLFormElement;
    // Every named input, _gotcha and the bot check's _botcheck included.
    const body = Object.fromEntries(new FormData(form));
    this.sending.set(true);
    this.http.post<Reply>(form.action, body, { headers: { Accept: 'application/json' } }).subscribe({
      next: () => {
        this.sending.set(false);
        this.problems.set([]);
        this.status.set('Thanks, your message is on its way.');
        form.reset();
      },
      error: (response: HttpErrorResponse) => {
        // A refusal (422, 429) carries our JSON; a network failure carries none.
        const reply: Reply | null = response.error?.ok === false ? response.error : null;
        this.sending.set(false);
        this.problems.set(reply?.errors ?? []);
        this.status.set(reply?.errors?.some((p) => p.field) ? '' : reply?.message ?? 'Could not send. Check your connection and try again.');
      },
    });
  }
}

Set it up

  1. 1

    Create a form in FormHaven

    Sign up, press New form and copy its address. It looks like https://formhaven.net/f/YOUR_FORM_ID.

  2. 2

    Provide HttpClient

    Add provideHttpClient() to the providers in src/app/app.config.ts, as shown below. HttpClient sends its requests with fetch by default.

  3. 3

    Add the component

    Save the code above as src/app/contact-form.ts with your address in the action. Add ContactForm to the imports of the page that shows it, and put <app-contact-form /> in that page's template.

  4. 4

    Send a test

    Run ng serve and open http://localhost:4200. Localhost is always allowed, so you can test before you deploy. Type a@b as the address to see the error appear under the field.

Provide HttpClient once

Once provideHttpClient() is in the providers, the component gets HttpClient with inject().

src/app/app.config.ts
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
import { provideHttpClient } from '@angular/common/http';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
  providers: [provideBrowserGlobalErrorListeners(), provideRouter(routes), provideHttpClient()],
};

A refused message arrives in error

HttpClient treats every status outside 200 to 299 as an error, so a refused message lands in the error callback: 422 for an address that is not valid, 429 when one address sends too often. response.error holds our JSON. Each entry in errors has a code, a message and, when one input caused it, the field. A network failure has no such body, so the component checks ok before it trusts one.

Reply for a mistyped address (422)
{
  "ok": false,
  "message": "The email address \"a@b\" is not valid.",
  "error": "The email address \"a@b\" is not valid.",
  "errors": [
    { "code": "TYPE_EMAIL", "field": "email", "message": "The email address \"a@b\" is not valid." }
  ]
}

Stop bots that skip the hidden field

Turn on the bot check in the form's settings and add this line once to src/index.html, inside <head>. It finds the form after Angular draws it, and the component sends its answer along, because new FormData(form) reads every named input, _botcheck included. Visitors see nothing, and it sets no cookies.

src/index.html
<script src="https://formhaven.net/bot-check.js" async></script>

Build and deploy

ng build writes the site to dist, in a browser folder under your project's name. Upload that folder to any host that serves static files. The form posts to FormHaven, so nothing has to run on the host.

Terminal
ng build
# upload dist/YOUR_PROJECT/browser to Netlify, Cloudflare Pages,
# GitHub Pages or any other static host

Questions about this setup

Which Angular version is this for?
It was tested on Angular 21.2 in a new app from ng new: a standalone component, signals and the @if block, posting to a FormHaven form. The @if block needs Angular 17 or later.
Can I use Reactive Forms or ngModel instead?
Yes. Send your form model as the body instead of the FormData object, and keep an empty _gotcha key in it. With the bot check on, also send the value of the _botcheck input that the script adds to the form element when it is sent.
Do I need a proxy or a CORS setting?
No. Form addresses accept requests from any website, so HttpClient can post from your own domain, a preview deployment or ng serve on localhost.
Is the form address a secret?
No. It is meant to sit in your bundle: it can receive messages, never read them. Spam filtering and the allowed sites setting decide what reaches you.
Can my client get the messages instead of me?
Yes. Add up to 5 more addresses to the form. Each one confirms by a link first, and their copy carries no FormHaven branding. You can turn your own copy off.
Where is the data stored?
On our servers in the EU, for 1 to 365 days as you choose per form, or not at all: then each message is emailed and its answers deleted. A data processing agreement covers every account.

More to read

JavaScript contact formThe same post with fetch and no framework React contact formThe same component in a React app Astro contact formA Contact.astro component for a static Astro site HTML form to emailOne line in the action, every message in your inbox Static site contact formOne form for Astro, Hugo, Eleventy, Jekyll or plain HTML Hugo contact formA partial and a shortcode for a static Hugo site Lovable contact formOne prompt for a site built with Lovable GDPR compliant contact formEU storage, a DPA and retention you set Kontaktformular nach DSGVOAuf Deutsch: gespeichert in Deutschland, mit AVV Vercel contact formFor plain HTML, Astro or Next.js hosted on Vercel FormSubmit not workingThe usual causes of a silent FormSubmit form Web3Forms not workingThe usual causes of a silent Web3Forms form Eleventy contact formAn include for a static Eleventy (11ty) site Formspree not workingThe usual causes of a silent Formspree form Export from FormspreeEvery Formspree submission, as a file Export from FormsparkEvery Formspark submission, as a file EmailJS not workingThe usual causes of a silent EmailJS form Form backend for AI agentsThe start request, every REST route and MCP tool Claude Code contact formOne prompt, then press Activate Cursor contact formThe prompt and the .cursor/mcp.json entry Contact form privacy policyThe paragraph to copy, in 8 languages GitHub Pages contact formFor user, organisation and project sites on github.io Cloudflare Pages contact formFor sites on pages.dev or your own domain Netlify contact formFor sites on netlify.app or your own domain Next.js contact formA client component that works with output: export Send form submissions to multiple emailsUp to 5 confirmed addresses per form, plus yours SvelteKit contact formOne Svelte 5 component, any adapter Nuxt contact formOne Vue component for Nuxt 4 and 3 Gatsby contact formOne component for a Gatsby 5 site HTML form webhookSigned JSON to your endpoint for every message Jekyll contact formAn include for Jekyll, on GitHub Pages or any host Contact form spamWhy forms get spam, and the layers that stop it Contact form honeypotThe hidden field that catches bots, and how to hide it Contact form without PHPFor hosts with no PHP, or mail() that lands in spam HTML form mailtoWhy action="mailto:" fails on webmail visitors, and the one-line fix Contact form templateThe same form in plain HTML Pricing€1 per form a month, €0.50 from the 11th

Get your form address in a minute

14 days free, no card. Then €1 per form a month, €0.50 from the 11th form.

Start your 14-day trial