Sanity
Learn
CoursesTyped content with Sanity TypeGenExtracting your schema
Track
Sanity Developer Certification

Typed content with Sanity TypeGen

Lesson
2

Extracting your schema

Learn how to extract your Sanity Studio schema into a JSON file. This is a crucial step before generating TypeScript types. This file, a static representation of your data's shape, will be used for type generation.

Log in to mark your progress for each Lesson and Task

Before generating TypeScript types, you need to extract the Sanity Studio schema written in JavaScript into a static representation, a JSON file describing the shape of the data your Sanity Studio produces.

Run the following Sanity CLI command in your studio project folder:

Terminal
npx sanity schema extract
Run sanity schema extract in the Studio project from your terminal

If your command ran successfully, you should see a new file called schema.json in your Studio project folder. In this course, you will only use this file for the type generation; you don’t need to do anything else with it.

If you explore its content, you will find your Studio schema described in a JSON format. For example, the document type for venue will look something like this:

schema.json
{
"name": "venue",
"type": "document",
"attributes": {
"_id": {
"type": "objectAttribute",
"value": {
"type": "string"
}
},
"_type": {
"type": "objectAttribute",
"value": {
"type": "string",
"value": "venue"
}
},
"_createdAt": {
"type": "objectAttribute",
"value": {
"type": "string"
}
},
"_updatedAt": {
"type": "objectAttribute",
"value": {
"type": "string"
}
},
"_rev": {
"type": "objectAttribute",
"value": {
"type": "string"
}
},
"name": {
"type": "objectAttribute",
"value": {
"type": "string"
},
"optional": true
},
"city": {
"type": "objectAttribute",
"value": {
"type": "string"
},
"optional": true
},
"country": {
"type": "objectAttribute",
"value": {
"type": "string"
},
"optional": true
}
}
}

This file can be used to develop tooling that needs to know about a Sanity Studio schema. But in this course, you will use it to generate TypeScript types.