How to use KV Store with Redis-compatible SDK
Learn how to use the Azion KV SDK with a Redis-like interface for seamless integration with your applications.
The Azion KV SDK provides a Redis-like client interface for interacting with KV Store. This makes it easier to migrate existing applications that use Redis patterns and provides a familiar developer experience.
Go to KV Store referenceRequirements
Before you begin, make sure you have:
- An Azion account.
- A KV Store namespace created via Console or API.
- Node.js 18+ or a compatible JavaScript runtime.
Installing the SDK
Install the Azion SDK using npm:
Creating a client
The SDK uses a Redis-like client pattern with chainable methods:
Client options
You can customize the client with options:
| Option | Type | Description |
|---|---|---|
namespace | string | The KV Store namespace to use |
apiToken | string | Your Azion API token (required for API provider) |
Storing data
Use the set method to store values:
Expiration types
| Type | Description |
|---|---|
EX | Expiration time in seconds |
PX | Expiration time in milliseconds |
EXAT | Unix timestamp in seconds |
PXAT | Unix timestamp in milliseconds |
Retrieving data
Use the get method to retrieve values:
Retrieving with metadata
Use getWithMetadata to get both the value and its metadata:
Deleting data
Use delete or del to remove keys:
Hash operations
The SDK provides Redis-compatible hash operations for working with field-value pairs:
hSet / HSET
Store a field-value pair:
hGetAll / HGETALL
Get all fields and values:
hVals / HVALS
Get all values (without field names):
Provider detection
The SDK automatically detects the runtime environment and selects the appropriate provider:
- Native provider: Used when running inside Azion Functions runtime
- API provider: Used when running outside Azion (local development, external servers)
You can check which provider is being used:
Error handling
The SDK uses an event-based error handling pattern:
You can also use try-catch for individual operations:
Disconnecting
Always disconnect when you’re done:
Complete example
Here’s a complete example showing common operations:
Redis method mapping
| Redis Command | SDK Method | Description |
|---|---|---|
GET | get(key) | Get a value |
SET | set(key, value, options?) | Set a value |
DEL | delete(key) / del(key) | Delete a key |
HSET | hSet(key, field, value) / HSET(...) | Set hash field |
HGETALL | hGetAll(key) / HGETALL(key) | Get all hash fields |
HVALS | hVals(key) / HVALS(key) | Get all hash values |
Next steps
- Learn more about KV Store
- Explore managing KV Store with Functions