This repo implements Netlify serverless functions to wrap third party APIs. After deploying, you can invoke an endpoint. The app builds and invokes a new URL concatenating two strings retrieved from environment variables and the original query string params. You can deploy yourself to manage sensitive information such as secret API keys without exposing them in your public repos or client side code. It is set up to be instantly deployed to Netlify!
- Demo site
- How it works
- Using this repo
- Roadmap
- Suggestions, bugs and whatever
- License
- Additional Learning materials
- Author
To see this function in action, here is deployed a live demo site that retrieves a JSON demo obj from REG|RES service for testing purpose.
Deploy and configure your own site in the simplest and quickest way possible by clicking this button:
If you do that, it will create a new repo for you that looks exactly like this one, and sets that repo up immediately for deployment on Netlify)
Once deployed, default behaviour is this:
-
You invoke your site's URL in that way: https://your-site-url.netlify.com/api?param1=value1¶m2=value2&....¶mN=valueN
-
The site builds a new Endpoint by concatenating
<MY_API_URL> + "?" + <MY_PARAM> + "param1=value1¶m2=value2&....¶mN=valueN"
-
The site invokes the new Endpoint
-
The site responses you with the response served in that endpoint
In the example, the built endpoint is https://reqres.in/api/users?page=2. You can set the values for environment variables MY_API_URL
and MY_PARAM
that fit your case navigating to your site's deploys and build environment settings in Netlify.
You can use the variables to set the endpoint and the secret API KEYS for third party APIs without publicly exposing them in your code.
Easiest way to deploy your own wrap is to click on the above Netlify Deploy Button and follow the guided steps. Once deployed, you will have to navigate to your new site's deploys and build environment settings in Netlify to set the environment variables that will make your case.
If you prefer do it manually, follow these few steps:
-
Verify you have git on your machine
To verify you have
git
, open up your terminal and run:which git
This should return a path of where git is installed. If the command returns
git not found
, we will need to install git on your machine. -
Clone this repo.
Open up your terminal and run:
git clone https://github.com/jota-ele-ene/wrap-api.git
-
Configure your own function.
Review the function code in
functions/api.js
file and check if it meets your needs. Commit your changes.You can see how we access the environment variables on the
process.env
object byprocess.env.MY_API_URL
andprocess.env.MY_PARAM
.After deploying the site in Netlify, you can set the values required for fitting your case.
-
Login to your Netlify site. If you don't already have an Netlify account, let's get one setup.
Create and deploy a new site by importing your new repo. Navigate to deploys and build environment settings. Here we need to set the environment variables we will access through our function. In the example, we use the REG|RES service to test:
-
MY_PARAM
=page=2
-
MY_API_URL
=https://reqres.in/api/users
See the build environment variable docs. These values are pre-filled if you chose to deploy with the Netlify Deploy button above.
-
-
Finally, verify the your function
Invoke the function to verify it is returning the expected response values
Open your terminal and run the following command:
curl -X GET https://your-site-url.netlify.com/api
Or visit the URL in your browser
It should redirect your request to the URL build by concatenating the values of
MY_API_URL
andMY_PARAM
and get its response. In the example, the invoked URL should behttps://reqres.in/api/users?page=2
.
Visit the To-do column in the basic kanban-style board open to manage this repo roadmap.
All feedback is welcome! Head over to the issue tracker.
This repo is licensed under the MIT license. Check the LICENSE file for details.
Thanks to the authors of following resources, among others, used for developing this code:
- Netlify Functions Docs
- Netlify Functions Examples
- Netlify Functions Tutorials
- Netlify Functions Serverless Workshop
José Luis Núñez https://joseluisnuñez.es
if you find this repo useful, enjoy it and please consider buying me a coffee ☕️.
Thanks! ❤️