Difference between _id and id in Sanity GraphQL

3 replies
Last updated: Apr 3, 2020
What's the difference between
_id
and
id
when using graphql?
AI Update

When using Sanity's GraphQL API, there's an important distinction to understand about document identifiers:

_id is Sanity's native document identifier field. This is a system field required on all documents stored in the Content Lake. Every document in Sanity has an _id field that uniquely identifies it. You can define your own _id when creating documents, but if you don't provide one, Sanity will auto-generate it. This is the field you'll use when querying Sanity's GraphQL API directly.

id is NOT a field that Sanity's GraphQL API generates. Based on the official GraphQL documentation, Sanity's GraphQL API only exposes _id as the document identifier field.

However, you might encounter an id field if you're using third-party tools or frameworks that add their own abstraction layer over Sanity's data. For example:

  • Gatsby with gatsby-source-sanity: The gatsby-source-sanity plugin creates an id field as part of Gatsby's internal node system for managing data in its GraphQL layer. This id is generated by Gatsby, not by Sanity.
  • Other source plugins or integrations may similarly create their own id fields for their internal purposes.

In practice:

  • When querying Sanity's GraphQL API directly, use _id - it's the only document identifier field available
  • If you see an id field, you're likely using a framework or plugin that's adding its own layer of abstraction (like Gatsby), and that id is specific to that tool's data management system

So the key takeaway: _id is Sanity's native identifier, while id (if present) comes from whatever framework or tool you're using to consume Sanity's data, not from Sanity itself.

Show original thread
3 replies
I'm pretty sure the difference is, that "id" is automatically generated no matter what. With "_id" you can define it yourself - but if you do not do that, it will be auto generated as well.
Gotcha, OK thanks
_id
is the document field that comes from Sanity and
id
is the node id that Gatsby generates :)

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.

Was this answer helpful?