Skip to Content
KingsPay Docs
API Key

Best Practices

Your API keys carry real access to your business account and should be treated with the same level of care as a password. Following these best practices helps protect your account and minimize the risk of unauthorized access.

Never expose your API keys

Never hardcode your API key directly in your source code, and never commit it to a version control system such as Git. If a key is accidentally pushed to a public repository, it should be considered compromised immediately. Instead, store your keys in a secure location that is separate from your codebase, such as environment variables or a secrets management service.

API keys should also never be exposed in client-side code, browser applications, or mobile apps, as anyone who can inspect or decompile your application would be able to extract the key.

Use environment variables

One of the most common and straightforward ways to keep your API key out of your codebase is to store it as an environment variable and reference it in your application at runtime. This keeps the key separate from your code and makes it easy to use different keys across different environments without changing your application.

Grant only the permissions you need

When creating an API key, only select the permissions that your integration actually requires. Avoid granting broad permissions as a convenience. The more permissions a key has, the greater the potential damage if it is ever leaked or misused. This principle is commonly referred to as the principle of least privilege.

Rotate your keys regularly

Even if you have no reason to believe a key has been compromised, rotating your API keys periodically is a good security habit. This limits the window of exposure in case a key was silently leaked without your knowledge. To rotate a key, delete the existing one and create a new one to replace it.

Revoke compromised keys immediately

If you suspect that an API key has been leaked or accessed by an unauthorized party, delete it immediately and replace it with a new one. Do not wait to confirm the leak before acting. The sooner a compromised key is revoked, the smaller the window of potential misuse.

On this page