Skip to content
EN

Saving external form submissions to CMS with Public API

Last updated

This guide explains how to save data submitted from an HTML form — such as a contact form or a survey form — to Re:Earth CMS. With Public API, you can post the form’s contents to the CMS without an API key or an integration token.

Below is sample code for a contact form. It submits a name and a comment to the CMS and saves them. Change values like WORKSPACE_ID and the keys in fields to match your own model’s configuration.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Contact</title>
</head>
<body>
<form id="contact-form">
<label>Name<br><input type="text" name="name" required /></label><br>
<label>Message<br><textarea name="comment" required></textarea></label><br>
<button type="submit">Submit</button>
</form>
<script>
const WORKSPACE_ID = "<workspace_id_or_alias>";
const PROJECT_ID = "<project_id_or_alias>";
const MODEL_ID = "<model_id_or_key>";
const url = `https://api.cms.reearth.io/api/p/${WORKSPACE_ID}/${PROJECT_ID}/${MODEL_ID}/items`;
document.getElementById("contact-form").addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const body = {
fields: {
name: formData.get("name"),
comment: formData.get("comment"),
},
};
const resp = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(body),
});
if (!resp.ok) {
const text = await resp.text();
throw new Error(`HTTP ${resp.status}: ${text}`);
}
alert("Submission complete.");
e.target.reset();
});
</script>
</body>
</html>

Filling in the form and submitting it creates an item via Public API.

A contact form with name and message fields and a Submit button.

Checking the CMS’s Content screen shows the submission saved as an item with Draft status.

Re:Earth CMS's Content screen, showing the form submission listed as an item with Draft status.

If you want to publish the item, either publish it from the CMS management console, or see Publishing data with Integration API.

For the full endpoint specification, see Public API Reference.