Using StaticForms with Next.js: A Complete Guide

7 min read
Static Forms Team

Are you building a Next.js application and need a simple way to handle form submissions without setting up a backend server? Static Forms is the perfect solution for collecting form data from your static sites and receiving submissions directly in your inbox.

In this guide, we'll explore how to integrate the StaticForms service with your Next.js application, including both basic implementation and enhanced security with reCAPTCHA. If you're using Nuxt.js instead, check out our Nuxt.js integration guide.

What is StaticForms?

StaticForms is a form-to-email service designed specifically for static websites. It allows you to receive form submissions via email without writing any server-side code. This is particularly useful for:

  • Contact forms
  • Feedback forms
  • Newsletter signups
  • Simple data collection

The service is free to use (with some limitations) and provides a straightforward API for integrating forms into your static website.

Getting Started with StaticForms

Before implementing forms in your Next.js app, you'll need to:

  1. Visit staticforms.dev
  2. Register for a free account
  3. Obtain your unique API key

This API key will be used to connect your forms to your StaticForms account, ensuring submissions are forwarded to your email address.

Basic Integration in Next.js

Let's start by implementing a simple contact form without reCAPTCHA. This approach is quick to set up but offers less protection against spam.

Create a Client Component for Your Form

TSX

Use the Form Component in Your Page

TSX

Enhanced Integration with reCAPTCHA

For better protection against spam and bot submissions, we can add Google reCAPTCHA to our form. This requires a few additional steps:

  1. Register for reCAPTCHA at Google reCAPTCHA and get your site key
  2. Add the reCAPTCHA component to your form

Contact Form with reCAPTCHA

TSX

Installing Required Dependencies

Don't forget to install the necessary packages:

Bash

Using Environment Variables for API Keys

For better security, it's recommended to use environment variables for your API keys instead of hardcoding them in your components:

  1. Create a .env.local file in your project root:
Plain Text
  1. Update your components to use these environment variables:
TSX
TSX

Advanced Configuration Options

StaticForms offers several additional configuration options for enhancing your forms:

Custom Success/Error Redirects

You can specify custom URLs to redirect users after form submission:

TSX

Custom Fields

You can add custom fields to your form by prefixing the field name with $:

TSX

Troubleshooting Common Issues

Form Submissions Not Received

If you're not receiving form submissions:

  1. Double-check your API key is correct
  2. Make sure your email address is verified with StaticForms
  3. Check your spam folder

reCAPTCHA Not Working

If reCAPTCHA isn't functioning properly:

  1. Verify your site key is correct
  2. Ensure your domain is listed in the reCAPTCHA admin console
  3. Check for any JavaScript errors in the console

Conclusion

Integrating StaticForms with Next.js provides a simple, effective solution for handling form submissions in static sites without setting up a backend server. Whether you use the basic implementation or enhance it with reCAPTCHA for extra security, StaticForms makes the process straightforward.

For more information about StaticForms, check out our getting started guide or explore other integration examples for different frameworks, such as our Nuxt.js integration tutorial.

Remember that the free tier has some limitations, but it's perfect for most personal projects and small business websites. For larger projects with higher submission volumes, consider upgrading to a paid plan.

Happy coding with Next.js and StaticForms!