{}

Submit JSON with Node.js

Submit JSON isn't only for handling static site form submissions.

In this example, we will use Node.js and Express to make a realtime notification when a new user signs up to our app.

Before we begin, make sure you are running Node version 18 or higher. If you are on a lower version, you'll have to use our REST API without the JS Client


Install the Submit JSON JS Client

First install submitjson with your favorite package manager.

npm i submitjson # OR pnpm add OR yarn add
shell

Integrate Submit JSON with your Express project

Next initialize the client and handle the new user sign up notification on a /register route.

import express, { Request, Response } from 'express'
import SubmitJSON from 'submitjson'

const app = express()
const port = 3000

const sj = new SubmitJSON({ apiKey: 'sjk_xxx', endpoint: 'XxXxXxXxX' })

app.post('/register', async (req: Request, res: Response) => {
  // ☝️ handle your register logic before this point
  // the data you submit can be any valid JSON object:
  const data = {
    notificationType: 'new-user',
    name: 'Yo Yoerson',
    email: '[email protected]',
    membershipType: 'trial',
    currentPeriodEnd: '2023-02-23',
  }
  // submit your notification, overriding the endpoint defaults
  const submission = await sj.submit(data, {
    emailNotification: true,
    emailReplyTo: data.email,
    submissionFormat: 'pretty',
  })
  // ⚡️ easy peasy
  console.log(submission)
  res.status(200).end()
})

app.listen(port, () => {
  console.log(`Server is running at http://localhost:${port}`)
})
ts

🚛✨💚 Nice job, you successfully integrated Submit JSON with Node.js and Express.