Setting / Getting redirects programmatically via API

Hello! As per title, is there a way to programmatically get/set redirects and rewrites for a site? I’m trying to do this as part of an automated deploy script, but failed to find anything.

My fallback approach was to download the latest deploy, parse the netlify.toml file, get / update the redirects table, then redeploy the site - but seems like a lot of effort so wanted to check if I have missed something, or if there’s a better way to do this.

Thank you!

No, there’s no programmatic way. You don’t need to download the entire deploy, you can just download your netlify.toml.

Or you could store your redirects in Netlify Blobs and implement the redirection logic using Edge Functions. Then you could update them on the fly.

Thanks for your reply. How can I download just the netlify.toml via the API? I tried using the endpoint https://api.netlify.com/api/v1/deploys/${latestDeploy.id}/files/netlify.toml but the response just returns a data structure with informations about the file, such as the path and the sha - and it doesn’t seem to be possible to download the netlify.toml directly file as a public asset from the site

You can download that file in the “Deploy file browser” at the bottom of the deploy details page for the site.

Thank you, is there a way of doing that via the API or the netlify CLI?

For posterity, I had to use GET /api/v1/sites/{site_id}/files/{file_path} but with application/vnd.bitballoon.v1.raw as the Content-Type, as specified in https://docs.netlify.com/api-and-cli-guides/api-guides/get-started-with-api/

Netlify doesn’t provide an API to directly add or update redirects/rewrites.
To change them, you must update your _redirects or netlify.toml file and then redeploy the site.

If you need dynamic redirects without redeploying, you’d have to handle them manually using an Edge Function.