I have created an api and I want to have some control over who uses it, how often etc. I want to have an API key strategy so that the users must provide the key in order to use the API. However i don't know how to implement it. The users are registered with username and password. What i thought of was to assign a UUID when the user logs in and store it in a table in the database. Then each request includes this uuid and it is checked on each request at the server.
However this does not seem right. Could someone explain the steps in order to create an api key like, dropbox, twitter, facebook etc. does? I want to try to implement this myself.
Don't store your API key directly in your code. Instead, store your API key and secret directly in your environment variables. Environment variables are dynamic objects whose values are set outside of the application. This will let you access them easily (by using the os.
Using an API key with REST You can pass the API key into a REST API call as a query parameter with the following format. Replace API_KEY with the key string of your API key. Alternatively, you can use the x-goog-api-key header to pass in your key. This header must be used with gRPC requests.
However, without a valid API key, Google won't answer your request. You need special permission. The API key lets Google know who you are and whether you have the right to access their map service. This is called authentication (as opposed to authorization, which we discuss later in the article.)
Could someone explain the steps in order to create a api key like, dropbox, twitter, facebook etc. does? I want to try implement this myself.
Note: if you don't want it to be a decryptable key, as in, it is hashed and thus infinitely more difficult to crack, then you can simply follow this stratgey: make a set of steps to form your unhashed data string: sha1("some-secret"."some-other-bit-of-info"."etc"."etc")
and then the API consumer has the onus on them to generate their own key. Thus, they have access only if they have the necessary parts / info needed to construct it.
Take Stripe's API as a decent example:
make authorization request: an API key is returned. "curl uses the -u flag to pass basic auth credentials (adding a colon after your API key will prevent it from asking you for a password)." --Stripe Docs
send that key along with all further requests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With