Cloudflare DevelopersCD
Cloudflare Developers3y ago
2 replies
dXtee

Pages Functions as API

Hi, looking for some help with functions to help me fetch json data from a third party API.

Apologies in advanced if I have got this all wrong, I am usually just front-end but I am helping this animal rescue organisation make their webpage - and to list the animals they have for adoption we are using a third party api that needs a API secret. To hide the API secret from the client-end I went about making an express.js backend that will be hosted and the end points will simply just call the
third party API with the secret, and some basic filtering queries will just be appeneded to the fetch.

As this is just a simple 'backend' I believe it would be best suited for serverless functions like on Cloudflare Pages Functions as I have migrated the static page to be hosted on Cloudflare Pages already. Would this implementation be possible in CloudFlare Pages functions. I tried looking through documentation and attempted to implement last night, would the basic functions act the same with a getRequest. I had issues with parsing the request for the queries as well as returning a proper response. An example query would be ".../listings?page=1&species=Cat"

If anyone has any examples they could point me to or any guidance I would greatly appreciate it.

Attached is an example endpoint for the express implementation.
Screenshot_2023-11-08_181159.png
Was this page helpful?