apikee v0.1.0
apikee
apikee
is a package designed to securely generate and validate API keys using encryption. It supports both local and managed API keys, providing encryption-based security and easy validation.
For more information about managed keys, visit apikee.com.
Table of Contents
Installation
To install the apikee
package, you can use npm or yarn:
npm install apikee
or
yarn add apikee
Configuration
Before using the package, configure it with the necessary environment variables. You can either set these in your environment (recommended) or configure them manually in the code.
Required Environment Variables:
APIKEE_SECRET
: (Mandatory) This is the master secret used for encryption. It is used to securely derive encryption keys.APIKEE_HOST
: (Optional) The host for your API server. This is used for managed keys.APIKEE_TOKEN
: (Optional) The token used to authenticate API requests. This is used for managed keys.APIKEE_PROJECT
: (Optional) The project ID associated with your API. This is used for managed keys.
Using Environment Variables
Set these variables in your .env
file (recommended) or in your environment:
APIKEE_SECRET=your-secret-here
APIKEE_HOST=your-api-host
APIKEE_TOKEN=your-api-token
APIKEE_PROJECT=your-project-id
Optional Manual Configuration
If you prefer not to use environment variables, you can manually configure them in your code:
import { configureApiKee } from 'apikee';
configureApiKee({
host: 'your-api-host',
token: 'your-api-token',
project: 'your-project-id'
});
Usage
Generate API Key
To generate an API key, you can use the generateApiKey
function. It supports both local and managed keys.
Example:
import { generateApiKey } from 'apikee';
const { apiKey, keyId } = await generateApiKey('my-key-name');
// Output
console.log('API Key:', apiKey);
console.log('Key ID:', keyId);
name
(string): The name of your key or service.projectId
(optional, string): The project ID for managed keys.endpointIds
(optional, array): A list of endpoint IDs for managed keys.
Notes:
- If you provide a
projectId
, it will generate a managed API key using the server. - If no
projectId
is provided, it will generate a local key. - If you don't specify
endpointIds
, all project endpoints will be considered.
Validate API Key
To validate an API key, use the validateApiKey
function. It will check if the provided key matches the expected value, using either the local key or the managed key system.
Example:
import { validateApiKey } from 'apikee';
const isValid = await validateApiKey('your-api-key', 'your-key-id');
if (isValid) {
console.log('API key is valid');
} else {
console.log('Invalid API key');
}
apikey
(string): The API key to validate.keyId
(string): The key ID to validate against for local keys ORendpointId
(string): The endpoint ID to validate against for managed keys.
Error Handling
If any validation or encryption steps fail, the functions will throw errors. Make sure to handle these errors properly to ensure smooth functionality.
Example:
try {
const { apiKey, keyId } = await generateApiKey('my-app-name');
// Continue processing with the generated API key
} catch (error) {
console.error('Error generating API key:', error.message);
}
License
This package is released under the MIT License. See LICENSE for more details. For more information about managed keys, visit apikee.com.