Quick way to Secure API Keys for the Frontend
Tags: showdev, webdev, security, vue
Published: 2021-09-29 20:26:50

---
title: Quick way to Secure API Keys for the Frontend
published: true
description: 
tags: showdev, webdev, security, vuejs,
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjh4feuhd6r8q7qcgez5.jpg
---

We all know that API keys and connections can not be secured on the client side of an application. Hard coding API keys on the frontend is a quick and surefire way to have your API connection shutdown, API keys stolen, and have your API provider’s bill skyrocket. So what options are there if you do not want to maintain back end infrastructure? We will explore the recommended techniques for integrating 3rd party APIs into client side applications without having to build a backend. Then we will walk you through a step by step example of integrating a private API to create a Covid 19 tracker using [KOR Connect](https://korconnect.io).   

##**Ways of integrating 3rd party APIs without backend infrastructure:**

![Lambda image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6ufft11nrvwv10knfon.png)
####**Serverless Functions as a backend proxy (AWS Lambda):** 
It is often recommended to use serverless functions to hide API keys for client side applications. Then the client can use this serverless function as a proxy to call the API through a new endpoint. The developer should also incorporate CORS to identify the header origin so that only the allowed domains are calling the proxy (to prevent unwanted calls to the proxy url from anywhere). This may seem secure but CORS only verifies browser calls and can be easily spoofed or can be called from outside of the browser. A malicious actor can still run up costs with a bot and have the endpoint shut down. Further issues with this technique can arise around provisioning AWS services to support the lambda functions like API gateways, roles, and permissions between cloud services, this can be very time consuming if you are not familiar with the cloud provider. 

![Netlify logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0q72a1o7nbcsnp61f95k.jpg)
####**Netlify Functions (built on AWS Lambda):** 
Netlify Functions is a wrapper around AWS Lambdas, the main advantage to using this approach over the AWS provisioned proxy is an improved user experience and Netlify helps streamline the deployment for you. Netlify Functions remove the tasks associated with setting up an AWS account and other AWS services required to correctly integrate the API. Similar security issues persist with Netlify Functions as they do with setting up your own AWS provisioned proxy. Even with CORS setup the new Netlify endpoint can be called in unwanted ways and by unwanted agents. This leaves your API susceptible to being shut down, or having costs run up. Furthermore if you are not familiar with writing functions this could present an additional learning curve.  

![KOR Logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fg4bj3hp3w310mkvzmcz.png)
####**[KOR Connect](https://korconnect.io):** 
KOR Connect is a new way for client-side web apps to integrate APIs. KOR Connect is the quickest way to secure API Keys and connect 3rd party APIs because you do not need to build infrastructure (AWS/ other cloud providers), or code functions (AWS and Netlify Functions). KOR Connect also uses AWS Lambda to secure API keys but the similarities between KOR Connect and the other options end there. The API key is secured on KOR Connect through a one click integration then a snippet containing a new public URL is copy-pasted into the developer’s code. This snippet that is placed into the frontend code contains Google’s Recaptcha V3 which is used as an attestation layer to confirm the origin of the endpoint call as well as block unwanted bot traffic. KOR Connect also has additional layers of security to further protect the API traffic from man-in-the-middle attacks. KOR Connect prevents endpoint calls from malicious actors with and without the browser, secures API keys, and blocks bot attacks. The public URL that is used in the code does not need to be hidden so this frees the developer from having to worry about API secrets ending up in the git repository, API secrets being exposed on the client, having to manually create wrappers around lambda functions, and worrying about unwanted endpoint calls being made. The current feature set KOR Connect is the best option for client-side web apps that want dynamic functionality but may not necessarily want user authentication. (Bonus it’s also free)

###**Now let's walk through an example using KOR Connect and Vue.js!**

![gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivbhc4t0wbj9p1n73ued.gif)
Lets create a COVID-19 tracker. In order to do this we have to pick the API that we want to use. I decided on the [COVID-19 Statistics API](https://rapidapi.com/axisbits-axisbits-default/api/covid-19-statistics/
) that uses data from John Hopkins University.  


If you already have a [KOR Connect](https://korconnect.io) account you can sign in [here](https://app.korconnect.io/login) or you can create a new account.

Let’s start by creating an API connection on KOR Connect by clicking on the “+ Connect API” button:
![Connect API Button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wg07xvqtu7udjuggvhem.jpeg)

The connection details were all copied directly from RapidAPI. More information regarding the API connection module [here.](https://kor-comunity.gitlab.io/kor-connect/adir/GETSTARTED.html)

![Connection module](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3a9i91j6vjvzah2pizmw.png)

Done! After making the connection, go to View Details for your new API connection.

![Snippets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1exv7dogze4zk0aikoa.png)



*If you want, [here](https://www.youtube.com/watch?v=m-MAIpnH9ag) is a great video tutorial by Traversy Media walking you through building the site on Vue.js. ([Here](https://github.com/bradtraversy/vue-covid-tracker) is the code for his COVID-19 tracker).
__Note:__ In the tutorial he uses a public API, we will use KOR Connect to easily integrate the private API into our site.*

Now, all you need to do is grab the Secure URL and Public API Key provided by KOR Connect and use them to make an API request. Here's an example of the fetch request. 

```javascript 
fetch("<YOUR-SECURE-URL>", {
	"method": "GET",
	"headers": {
		"x-rapidapi-key": "<YOUR-PUBLIC-API-KEY>"
	}
})
.then(response => {
	console.log(response);
})
.catch(err => {
	console.error(err);
});
```

It was as simple as that, now your API integration works without any additional libraries or configurations.

If you're interested in adding even more security through an attestation service keep reading. 

## Additional Security
Let’s take a look at how KOR Connect provides an additional layer of security. Follow along if you would like to implement Recaptcha as an attestation layer amongst other security features. 

You will have to navigate to the Additional Security section within the View Details of your API connection. 

![Additional_Security](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uv1f18b9lxqghpojsz0y.png)

Then you will have to turn on Additional Security. When Additional Security is turned on, the Security Type should read Single URL + Additional Security.

![Toggle_additional_sec](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpvfm1010ubogsweoh1f.png)

Now, scroll down to the snippets section and depending on what framework you are using for your project, you will add the corresponding snippets of code to your project. We will be walking though this Covid19 tracker API integration using VueJS. 

![Vue_snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cvteor39uzngqc1tluir.png)

Take a look at the following snippet:
![Snippet box](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tg2w3d0a3wf55lhxpgh0.jpeg)

This is how to integrated the snippet into your project:

![Modified snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8evdz2qu9yhqr01pzzb5.png)

**Note:** the KOR Connect URL is modified with the paths I received from RapidAPI. For example, to get the total Covid cases I need to add this to my base URL:

![Path](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/az6j8mssouab1cihrjmx.png)

*All the paths available in the API you are using can be appended to the KOR Connect base URL (secure URL).*

Now, all of the API calls are made to the public URL that KOR Connect provides. KOR Connect will act as a proxy to authenticate as well as send the API information. Furthermore, thanks to reCaptcha V3 (which is implemented automatically) and additional layers of security, several malicious attack vectors are blocked which enhances KOR Connects security.
 

![Finished site](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zj4ttldiae61fbmtniyb.jpeg)

*With permission, this blog references this [Covid 19 tracker blog](https://dev.to/korconnect/secure-api-keys-on-the-frontend-vue-js-1l21) post made by Rodrigo.*  

**Here are a few for more cool project tutorials using [KOR Connect](https://korconnect.io):**  

[Connecting the YouTube API to download music without a backend on Angular](https://dev.to/korconnect/integrating-youtube-s-api-on-angular-to-download-music-without-a-backend-3bof)

[Setting up a Gif generator API with on Reactjs](https://dev.to/korconnect/connecting-a-gif-api-on-reactjs-without-a-backend-3fj7) 

[Integrating a real time currency exchange rate calculator API - HTML](https://dev.to/korconnect/integrate-apis-with-your-html-site-without-a-backend-2303)