Nomadcoders

API key, hide safely 5 min cut!

thumbnail

Title: Secure Your API Keys with Cloudflare Workers

Introduction

When building a project that uses a third-party API, they usually provide an API key. If you don't have a server, you might end up putting those API keys on the code that is going to the browser or phone of the user. This is not secure since anyone with technical skills can extract your API keys. The only place where your API keys are protected is on your server. However, managing a server just for protecting a couple of API keys is a lot of effort.

Solution

Cloudflare Workers Cluster is a fast and easy way to deploy code to the cloud. You only have to write a JavaScript or TypeScript function, run one command and the code will be deployed on the Cloudflare Network. You can protect your API keys for free with Cloudflare Workers.

Steps

  1. Create a Cloudflare Cluster Workers account.
  2. Install Wrangler CLI.
  3. Login to Cloudflare with Wrangler.
  4. Create a keys folder with Wrangler.
  5. Write a JavaScript or TypeScript function to send requests to the third-party API while hiding the API key.
  6. Run Wrangler Dev to get a URL to test the function.
  7. Publish the worker by running Wrangler publish.

Conclusion

Cloudflare Workers is a simple and secure way to protect your API keys without managing a server. You only pay for what you use, so you won't have to spend a lot of money. It is also free for small projects with less than 100,000 requests per day. Try it out and keep your API keys safe!