API key just for a specific environment?

Hello,

Is there a way to bind an API key to a certain environment?
Say that we have two environment - staging & production, both in Dato, as well as on our backend server.

I know we can create/have multiple API keys in a certain environment, but currently it looks like that the API keys are the same across multiple environments, so I’m assuming the API key our backend server uses can only fetch Dato content from a primary environment?

If this is not possible, what would be the best way to have staging & production environments in Dato? Is the only option to duplicate the project?

Thank you for time,

Mauro

Hello @benec

The best way to do it is to set the permission of the role that the Token inherits permissions from, to only allow it to interact with sandbox environments.

Then, create a token that inherits that role:

This only allows for you to create tokens that modify either the primary environment only, or only sandbox environments, if you want more granularity, perhaps splitting the project into multiple projects would be a better idea

Replying here in case someone else stumbles across this and wastes a ton of time like I just did…

It sounds like you just want your staging deployment to pull from the sandbox environment while your live site pulls from the primary environment – that’s how I ended up on this page.

You don’t need to create a new role and a new token as it says above, you just need to specify the environment in the headers object, as detailed here: Content Delivery API - API endpoint and header modes - DatoCMS Docs

thank you @webservices for the update. Your solution is fine if you don’t mind sharing a token that can access all the environments. The messages above set up an API token that cannot access the other environment, so you can decide who has access to what.

If you don’t need that complexity, surely you can use the same token and only specify from which environment to get data from.