Headless CMS setup guide for HTML/CSS/JS developers without frameworks
Again, maybe it's only because I'm too unskilled to get it, I don't know.
You don't need React or a framework to use Sanity. Sanity Studio is the editing app, and it needs Node.js to set up. Your portfolio can stay plain HTML, CSS and JavaScript and get its content with fetch() from the HTTP Query API, which returns JSON. A page in the browser also needs its address added to the project's CORS origins, which is the step people most often miss.
1. Create the Studio (one time, needs Node.js)
The installation docs call for Node.js v22.12 or newer:
npm create sanity@latest -- --dataset production --template clean --output-path studio
cd studio
npm run devOpen http://localhost:3333 and sign in with the same account you used in the CLI (quickstart).
2. Add a "project" schema
The clean template starts with no document types, so define one in schemaTypes/projectType.ts:
import {defineField, defineType} from 'sanity'
export const projectType = defineType({
name: 'project',
title: 'Project',
type: 'document',
fields: [
defineField({name: 'title', type: 'string'}),
defineField({name: 'description', type: 'text'}),
defineField({name: 'image', type: 'image'}),
],
})Register it in schemaTypes/index.ts with export const schemaTypes = [projectType]. The Studio reloads and shows "Project" (schema lesson). Add a few projects and publish them.
To host the Studio at your-name.sanity.studio, run npx sanity@latest deploy (deploy reference).
3. Allow your website's origin (CORS)
By default only localhost:3333 and your deployed Studio host are allowed. For your site to read a public dataset from the browser, add its origin (CORS docs):
- Go to sanity.io/manage → your project → Settings → API settings → CORS Origins → Add CORS origin. Enter, for example,
https://your-domain.comandhttp://localhost:5500(whatever your local server uses), and leave Allow credentials off. - Or, from the studio folder:
npx sanity@latest cors add https://your-domain.com
4. Fetch content with plain JavaScript
URLs have the form https://<projectId>.apicdn.sanity.io/v<YYYY-MM-DD>/data/query/<dataset>?query=.... The API CDN host (apicdn) is cached and fine for public reads. The version is a date; use today's date (HTTP API reference). The response JSON has your data in result.
<!DOCTYPE html>
<html>
<body>
<h1>My Projects</h1>
<div id="projects"></div>
<script>
const projectId = 'YOUR_PROJECT_ID' // shown in sanity.io/manage
const dataset = 'production'
const query = `*[_type == "project"] | order(_createdAt desc){
title, description, "imageUrl": image.asset->url
}`
const url = `https://${projectId}.apicdn.sanity.io/v2025-02-19/data/query/${dataset}?query=${encodeURIComponent(query)}`
fetch(url)
.then((res) => res.json())
.then(({result}) => {
const container = document.getElementById('projects')
result.forEach((project) => {
const div = document.createElement('div')
const h2 = document.createElement('h2')
h2.textContent = project.title
const p = document.createElement('p')
p.textContent = project.description || ''
div.append(h2, p)
if (project.imageUrl) {
const img = document.createElement('img')
img.src = `${project.imageUrl}?w=800&fit=max&auto=format`
img.alt = project.title || ''
div.append(img)
}
container.append(div)
})
})
.catch((err) => console.error(err))
</script>
</body>
</html>Images
Don't take apart the asset _ref by hand. Ask for the URL in the query with image.asset->url and add parameters such as ?w=800, &fit=max (never upscale) or ?auto=format. Loading unoptimized originals can cause usage overages (presenting images). If you later add a build step, @sanity/image-url builds these URLs for you and handles crop and hotspot.
Things to know
- Datasets are public by default, so no token is needed. If you make the dataset private, don't put a token in browser code. Use a small server that holds the token instead.
- GROQ basics (cheat sheet):
*[_type == "project"]returns all projects;[0..5]returns the first 6 (inclusive) and[0...5]the first 5;{title, description}returns only those fields;->follows a reference. - A query that matches nothing returns
[], not a 404, so if you get an empty array, check the_typename and that the documents are published.
Show original thread6 replies
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
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.


