
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeconst sanityClient = require('@sanity/client')const client = sanityClient({ projectId: 'xxxxx',
dataset: 'production',
apiVersion: '2021-03-25',
useCdn: false,
token:
process.env.SANITY_API_WRITE_TOKEN ||
'xxxxx',
})
export default async function addDebt(_req_, _res_) {const { lender,
amountFunded,
balance,
dateFunded,
frequency,
numberOfPayments,
payback,
status,
} = JSON.parse(_req_.body)
try { await client.create({ _type: 'debt',
lender,
amountFunded,
balance,
dateFunded,
frequency,
numberOfPayments,
payback,
status,
})
} catch (err) { console.error(err)`return
Couldn't submit debt, err })`
}`return
Debt ${_res_._id} has been created })`}
import { useState } from 'react'import { useForm } from ‘react-hook-form’_data_) => { setIsSubmitting(true)_data_)try {
_data_),type: ‘application/json’,
mb-3 px-3 py-2 w-full border-2 border-gray-300 rounded shadow-sm ${
errors.lender ? ‘ring-2 ring-red-500’ : null
}mb-3 px-3 py-2 w-full border-2 border-gray-300 rounded shadow-sm ${
errors.amountFunded ? ‘ring-2 ring-red-500’ : null
}mb-3 px-3 py-2 w-full border-2 border-gray-300 rounded shadow-sm ${
errors.dateFunded ? ‘ring-2 ring-red-500’ : null
}mb-3 px-3 py-2 w-full border-2 border-gray-300 rounded shadow-sm ${
errors.balance ? ‘ring-2 ring-red-500’ : null
}mb-3 px-3 py-2 w-full border-2 border-gray-300 rounded shadow-sm ${
errors.payback ? ‘ring-2 ring-red-500’ : null
}mb-3 px-3 py-2 w-full border-2 border-gray-300 rounded shadow-sm ${
errors.payment ? ‘ring-2 ring-red-500’ : null
}mb-3 px-3 py-2 w-full border-2 border-gray-300 bg-white rounded shadow-sm ${
errors.frequency ? ‘ring-2 ring-red-500’ : null
}mb-3 px-3 py-2 w-full border-2 border-gray-300 rounded shadow-sm ${
errors.numberOfPayments ? ‘ring-2 ring-red-500’ : null
}mb-3 px-3 py-2 w-full border-2 border-gray-300 bg-white rounded shadow-sm ${
errors.status ? ‘ring-2 ring-red-500’ : null
}//client.js
import {createClient} from 'next-sanity'
export const sanityClient = createClient({
projectId: 'your-project-id-from-sanity.json',
dataset: 'your-dataset-name',
apiVersion: '2021-03-25',
token: '<http://sanity-auth-token-from-manage.sanity.io|sanity-auth-token-from-manage.sanity.io>',
useCdn: true, // `false` if you want to ensure fresh data
})onSubmitfunction, you need to create an object that contains the fields in your form and their values, then use
client.create. It's surprisingly straightforward if you already have the JS Client configured. Here's a pseudo code version of the script you'll need to run:
import { sanityClient } from './client'
const onSubmit = async () => {
const doc = {
_id = uuid4(), //or whatever package you use for generating a random ID
...formData //if the fields/values in data match the fields in your Sanity Schema you can use the spread operator to add them to your doc
}
client.create(doc).then(res => console.log(`Document was created, document ID is ${res._id}`)
}Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store