Sometimes getting error "The supplied set of access grants is not valid" when creating a meta def.

Using the Admin GraphQL API v2025-01, I am sometimes getting an error "“The supplied set of access grants is not valid” when trying to create a new metafield. If I retry the request w/o changing anything, then sometimes it succeeds. I am using:

@shopify/shopify-api v11.10.0

Here is my code:

export const CREATE_METAFIELD_DEFINITION = `
  mutation CreateMetafieldDefinition($definition: MetafieldDefinitionInput!) {
    metafieldDefinitionCreate(definition: $definition) {
      createdDefinition {
        id
        name
      }
      userErrors {
        field
        message
        code
      }
    }
  }
`;

const client = new shopify.clients.Graphql(<GraphqlClientParams>{ session });

const res = await client.request(CREATE_METAFIELD_DEFINITION, {
  variables: {
    definition: {
      key: "custom",
      name: "Custom Bundle?",
      ownerType: "PRODUCT",
      type: "boolean",
      access: {
        admin: "MERCHANT_READ",
        storefront: "PUBLIC_READ",
      },
      pin: true,
    },
  },
})

console.log(res.data) // Seeing the error in returned userErrors

Here are a few steps and considerations to troubleshoot and potentially resolve this issue:

  1. Review Required Access Scopes: Ensure that your app has the required scopes to create metafields on the Shopify store. For managing metafields, generally, scopes like write_products or write_metafields (if available) are necessary. You can check the required scopes in the Shopify API documentation and compare them with what your app currently has authorized.

  2. Validate Access Grants: The access object in your mutation specifies who can read or write the metafield. In your case, you’ve set:

access: {
  admin: "MERCHANT_READ",
  storefront: "PUBLIC_READ",
}

Make sure that these access levels are correct and supported. For instance, MERCHANT_READ may not be a valid option depending on the context or API version. You might need to use read_access or a different term. Refer to the latest Shopify API documentation for the correct parameters.

  • API Version: Confirm that the API version (2025-01 in your case) you are using supports the operations and schema you are attempting to use. Sometimes, features or specific functionalities can change with new API versions, so it’s important to ensure compatibility.

  • Retry Logic: Since you mentioned that retrying the request sometimes results in success, this could indicate a temporary issue with the Shopify API or how requests from your app are being handled. Implementing robust error handling and retry logic in your application can help manage these intermittent failures more gracefully.

  • Check for API Changes or Outages: Occasionally, API errors can stem from changes made by Shopify or from temporary outages or issues on their end. Checking Shopify’s developer changelog and status page might provide insights if there’s an ongoing issue affecting the API’s behavior.

  • Examine the Full Error Response: Make sure to log or examine the full error response from Shopify. Sometimes, the userErrors array can contain additional details that can help you understand more about what exactly is invalid about the request.

Here’s a slight modification to add error handling and check the full error response more effectively:

try {
  const res = await client.request(CREATE_METAFIELD_DEFINITION, {
    definition: {
      key: "custom",
      name: "Custom Bundle?",
      ownerType: "PRODUCT",
      type: "boolean",
      access: {
        admin: "MERCHANT_MANAGE", // Adjusted from MERCHANT_READ to MERCHANT_MANAGE
        storefront: "PUBLIC_READ",
      },
      pin: true,
    },
  });
  console.log(res.data);
} catch (error) {
  console.error("Error when creating metafield definition:", error);
  // Additional error handling or retry logic here
}

Check the API documentation for the exact wording and options for access controls, and adjust accordingly. If the issue persists after checking these factors, consider reaching out to Shopify support with specific details about the issue, including request IDs and timestamps, to get more targeted help.

Don’t think the response you got previously worked, seems like it was ai

Have you found a solution for this? I’m experiencing the same and after updating to the suggested access grants it still does that sometimes. I have to wrap my functions that do that in a retry mechanism and it usually does it after 2nd or 3rd retry.

I’m also currently experiencing this issue, did you find a solution? @sameeranand

I’ve updated the access grants used across my app to the latest recommended ones and it still does it.

I’ve had to wrap my functions that do this in a retry mechanism. After the 2nd or 3rd retry it usually works, but would like to get this fixed.

PS: I don’t think the previous answer you got here was helpful, seemed artificial.