Sanity 400 bad request error: "object or function expected" overnight

4 replies
Last updated: Jun 8, 2023
Have there been any issues / updates overnight or yesterday? All of a sudden I am getting a 400 bad error returned with the error message
object or function expected
. I run the same builds overnight every night and they all worked 24 hours ago. Now I get that 400 error on basically every request I make.
AI Update

Based on my investigation, Sanity's status page shows all systems operational with no reported outages or incidents in the last 24 hours. The error message you're seeing - "object or function expected" with a 400 Bad Request - is actually quite unusual for Sanity API responses, which typically return structured JSON error messages.

Here are a few things to check:

1. Check if you're using third-party APIs in your build The error message "object or function expected" sounds more like a JavaScript runtime error or a response from a different API service. If your builds use services like OpenAI, Azure, or other third-party APIs, check their status pages as this error pattern is more consistent with those services.

2. Verify your Sanity client configuration Check if there have been any changes to:

  • Your API tokens or credentials
  • Project ID or dataset name
  • API version in your client configuration
  • Any environment variables that might have expired or changed

3. Look at recent API version changes Sanity recently changed the default API perspective from raw to published in API version 2025-02-19. If your builds recently started using this API version without explicitly setting a perspective, you might be getting unexpected responses. Try explicitly setting the perspective in your client:

const client = createClient({
  // ... other config
  perspective: 'raw', // or 'published'
  apiVersion: '2024-01-01' // use a specific version
})

4. Check for GROQ query syntax issues If you're using newer tooling, make sure your GROQ queries are wrapped with defineQuery if you're using TypeGen, as this is now required.

5. Inspect the actual error response The "object or function expected" error is very generic. Can you check:

  • The full error response body
  • Which specific endpoint is returning this error
  • Your network tab to see the actual HTTP response

Since your builds worked 24 hours ago and Sanity's status page shows no incidents, this is likely either a configuration issue on your end or a problem with a third-party service your builds depend on. If you can share more details about which specific requests are failing (the actual API endpoint, request payload, and full error response), that would help narrow down the issue!

Show original thread
4 replies
I've been experiencing the same, unfortunately I've found it to be a bit random with something like 1 in 20 requests failing with that response.I've temporarily added retry logic on each request that has allowed our builds to succeed but it's obviously not ideal.
Can you share more context about this?
We shipped a fix for this about 30 mins ago!
Thank you!

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?