Fetch Examples
Here are some examples of backends for commonly requested Fetch use-cases. In many cases, APIs set up to be called from a front-end with generally work out of the box with Fetch. This means much of the documentation and examples around the web for small backend workers will also likely apply here.
A Simple Backend
At its simplest, a backend can be a small function that returns data. Fetch makes a GET request to the endpoint URL, the backend runs the code in the function, then returns the response with the JSON data. This code will work as-is in most javascript backend setups. you can try it out in the Cloudflare Worker Playground or fork the Val Town.
API Wrapper
The following is an example of wrapping the use of an API in a small backend function. This pattern is useful for preparing data before using in Framer, and is used in the Fetch tutorial video. It follows the pattern of calling a source API, parsing the data, setting up the CORS headers, and then responding with data.
Configuring CORS
Like with all static websites, any API endpoints that are used with Fetch need to be configured to be safely accessed from within the Framer editor, as well as from the published site it will be used on. This means you’ll likely need to configure the CORS of your backend. Many tools and platforms such as Hono or Vercel have built-in helps to work with CORS, but here is simple implementation of this in a worker.
Authentication: Token
Lets say we want to use an API like IPinfo, which requires an API key to work. This is impossible to use securely on the front-end, so we need to use a worker in-between the API and Fetch to safely store our API token. To do this, you’ll want to store the token in an environment variable, and use that within your worker.
Using Hono
If you’d like to introduce a small library into the mix, Hono can help you easily manage multiple API routes. This can be helpful for things like OAuth that require you to serve endpoints for /callback
and more. Additionally it has an easy way to manage CORS with its cors()
helper.