Proxying endpoints to your backend

Posted on April 28th, 2020 • 2 min read

Static sites are great, but there are times you still need a backend. For example, you might have an API endpoint you want to keep hitting or a contact form you want to continue using. While we encourage you to use Jamstack-friendly alternatives instead of your backend (like StaticKit for forms or Commento for comments), we understand this might not always be possible, and thus provide you with an alternative.

Enter proxying: proxying allows you to forward all calls from an endpoint of your choice to any URL. For example, you might want to forward /contact to https://backend.myapp.com/contact. This solution not only allows you to continue using your endpoints as is your site wasn't static but can also take care of CORS and CSP issues you might encounter when using external endpoints.

To configure new rules for proxying or redirecting, click the Create Rule button in the Proxying & Redirects section of your Site Settings. You'll be asked to select the type of rule, the path affected by the rule, and the destination URL.

A screenshot of the Create Rule modal

Advanced Usage

Instead of creating a rule for each file in a folder you want to redirect to or for each API endpoint you want to forward, you can use regex.

For example, imagine you want to forward all API requests to your backend. If your API lives at https://example.com/api, you can set your path to /api/(.*) to match any URL that starts with /api. Then, if we set our destination to https://backend.example.com/api/$1, everything that comes after api/ in the original URL will be used to craft the destination URL, making https://example.com/api/user proxy to https://backend.example.com/api/user.