API keys are a great way to protect your APIs, which is why Deta has them built into Micros. API keys will work with any framework that runs on the Micros – no coding required from your end!
To use API keys with your Micro, first you need to enable Deta Access by running the following command from the root directory of your Micro's source code.
Now your API is protected by Deta Access and only accessible by you. Next, you need to create an API key to authenticate your requests.
To create a new API key, run the following command, make sure to provide at least the
For more info, refer to the
deta auth create-keydocs.
This will send a response similar to this:
In this example, the API key would be
The key will only be shown to you once, make sure to save it in a secure place.
API keys are passwords
Anyone with this API key can access your Micro, make sure to treat it as a password and save it in a secure place like a password manager.
Simply add this key to the header of every request you make to your Micro.
For example, suppose your Micro has an endpoint
/tut that responds with a simple string
Hello. You can set the value of
X-API-Key header along with your request like the following:
That is all you need to protect your APIs!