Skip to main content
Version: Next


Release Discord Test Security Linter

A NATS Key/Value storage driver.

Note: Requires Go 1.20 and above

Table of Contents


func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() (*nats.Conn, jetstream.KeyValue)
func (s *Storage) Keys() ([]string, error)


NATS Key/Value Store driver is tested on the 2 last Go versions with support for modules. So make sure to initialize one first if you didn't do that yet:

go mod init<user>/<repo>

And then install the nats implementation:

go get


Import the storage package.

import ""

You can use the following options to create a storage driver:

// Initialize default config
store := nats.New()

// Initialize custom config
store := nats.New(Config{
URLs: "nats://",
NatsOptions: []nats.Option{
// Enable TLS by specifying RootCAs
KeyValueConfig: jetstream.KeyValueConfig{
Bucket: "test",
Storage: jetstream.MemoryStorage,


type Config struct {
// Nats URLs, default "nats://". Can be comma separated list for multiple servers
URLs string
// Nats connection options. See nats_test.go for an example of how to use this.
NatsOptions []nats.Option
// Nats connection name
ClientName string
// Nats context
Context context.Context
// Nats key value config
KeyValueConfig jetstream.KeyValueConfig
// Wait for connection to be established, default: 100ms
WaitForConnection time.Duration

Default Config

var ConfigDefault = Config{
URLs: nats.DefaultURL,
Context: context.Background(),
ClientName: "fiber_storage",
KeyValueConfig: jetstream.KeyValueConfig{
Bucket: "fiber_storage",
WaitForConnection: 100 * time.Millisecond,