Discussion about unpublished data coming through Sanity's API and how to filter it out.

12 replies
Last updated: May 26, 2022
Was anyone going to respond to this
user A
? đŸ€”
May 26, 2022, 5:38 PM
Yes, we aim to look at every thread that’s posted and respond to as many as possible. We’re only 2-3 hours into our workdays so we appreciate your patience. 🙂
May 26, 2022, 5:51 PM
Thanks
May 26, 2022, 5:52 PM
Any chance someone could take a look at this please?
We’re having production issues due to
unpublished data coming through Sanity’s API.
I know y’all are busy, but it’s been hours

😕
May 26, 2022, 6:46 PM
I don’t understand the relation between the screenshot and what seems to be the issue. If you’re using a token, you’ll get drafts back from a fetch, so you’d need to include
&& !(_id in path("drafts.**"))
in your filter.
May 26, 2022, 6:49 PM
I just thought that something that was “deleted”, and then somehow “came back”, might have caused the unpublished content to come back from the API

Wasn’t aware that drafts came back w/ queries.. what do you mean, “using a token”?
May 26, 2022, 6:55 PM
is there anyway to get the published status of pieces of content in that response so we can just filter them out?
May 26, 2022, 6:56 PM
Ah.. so if I don’t send the auth token, then those draft items are filtered out.
Got it.
May 26, 2022, 7:10 PM
await sanity.fetch(`*[_type == "place" && !(_id in path("drafts.**"))]`)
This will filter out all the drafts, you should only get published results
May 26, 2022, 7:10 PM
Thank you
May 26, 2022, 7:10 PM
To your original question “Which I wasn’t expecting due to the fact that it’s “unpublished”. Is the API supposed to be returning _un_published content??”
The answer is yes, it will show whatever you have asked of, which by default is drafts as well as published documents. You would need to explicitly query for everything “without drafts” as User showed above.
May 26, 2022, 7:11 PM
To your original question “Which I wasn’t expecting due to the fact that it’s “unpublished”. Is the API supposed to be returning _un_published content??”
The answer is yes, it will show whatever you have asked of, which by default is drafts as well as published documents. You would need to explicitly query for everything “without drafts” as User showed above.
May 26, 2022, 7:11 PM
Whoops, just saw your response
user G
, thanks for popping in! 🎯
May 26, 2022, 7:11 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?