Blueprints

Define a CORS origin with Blueprints

Blueprints allow you to define and manage your CORS origins in code, then deploy them in a predictable manner.

CORS is a security mechanism that allows browsers to safely make requests across different origins. Without it, the browser's Same-Origin Policy would block your Sanity Studio from communicating with the Sanity API, since they run on different domains.

In this guide, you’ll define a CORS origin resource with Blueprints and deploy the blueprint to Sanity.

Prerequisites:

  • The latest version of sanity CLI is recommended to interact with Blueprints. You can always run the latest CLI commands with npx sanity@latest.
  • An existing project and a role with permission to edit a project’s CORS origins (requires the sanity-project-cors permission).
  • CORS support was first introduced in @sanity/blueprints v0.11.0. We recommend using the latest version of the library.
Before you can define a CORS origin, you need a blueprint.

Initialize a new blueprint

If you already have a blueprint, skip to the next section. To initialize a blueprint and a project-scoped stack in the current directory, run:

The command creates a sanity.blueprint.ts file in the current directory and a new stack in your project:

  • <your-project-id>: Your project ID. To list your projects and their IDs, run npx sanity@latest projects list.
  • --type ts: The blueprint file format. Use js or json for a JavaScript or JSON file.
  • --stack-name production: The name of the new stack. Use any name.

Configure the CORS origin

Use the defineCorsOrigin helper to define an origin.

A full list of available configuration options is available in the reference documentation.

Deploy the blueprint

Next, deploy the blueprint.

Once the deployment finishes, your CORS origin resource is active.

If you need to make changes, update the blueprint file (sanity.blueprint.ts) and run the deploy command again.

Destroy a deployed blueprint

Sometimes you want to remove a deployed resource so it won't run anymore or affect any future usage quotas.

To remove a resources created with a blueprint, you need to either:

  • Remove the definition from the blueprint, and run the deploy command again.
  • Destroy the blueprint with the destroy command.

The blueprints destroy command removes, or undeploys, the blueprint and all of its resources from Sanity's infrastructure. It does not remove your local files.

To remove the resource from the blueprint locally, you can remove it from the resources array in the sanity.blueprint.ts file, then delete any associated files.

Redeploying a destroyed blueprint

When you run blueprints destroy, it's as if you never used blueprints init during setup. The only difference is you still have all the files in your directory. To use this blueprint again and redeploy it, you'll need to let Sanity know about it. You can do this by running init again:

This launches an editing interface that lets you reconfigure the blueprint, if needed, and it reconnects the blueprint to Sanity. Now you can add more functions or redeploy. Keep in mind that any environment variables added before destroying the blueprint will not carry over.

Learn more about CORS

Was this page helpful?