Skip to main content

natsio-natscontext

Connect natsio using the connection profiles ("contexts") managed by the nats command-line tool — the same JSON files under ~/.config/nats/context/. This is the Python counterpart of the Go helper orbit.go/natscontext, following ADR-21.

pip install natsio-natscontext

Usage

Create a context with the CLI, then use it from Python:

nats context add staging --server nats://staging:4222 --creds ~/staging.creds
nats context select staging
import natsio.natscontext as natscontext

# Connect using a named context...
async with await natscontext.connect("staging") as nc:
    await nc.publish("greet", b"hi")

# ...or the currently selected context (from `nats context select`).
async with await natscontext.connect() as nc:
    ...

# Overrides are forwarded to natsio.connect and win over the context:
nc = await natscontext.connect("staging", name="my-app", ping_interval=30)

Inspect or reuse a context without connecting:

ctx = natscontext.load("staging")          # -> Context
print(ctx.url, ctx.jetstream_domain)
kwargs = ctx.connect_kwargs()              # dict of natsio.connect(**kwargs)

natscontext.list_contexts()                # ['prod', 'staging', ...]
natscontext.selected_context()             # 'staging' or None

An absolute path is loaded directly instead of by name:

ctx = natscontext.load("/etc/nats/prod.json")

Discovery

Contexts are resolved under $XDG_CONFIG_HOME/nats (falling back to ~/.config/nats):

Path Purpose
nats/context/<name>.json one context per file
nats/context.txt name of the active/selected context

Field mapping

Context field natsio option Notes
url servers split on , into a tuple
user + password user, password
creds credentials ~ and $VAR expanded
nkey nkey_seed the file is read and the seed extracted
token token
cert + key tls SSLContext.load_cert_chain
ca tls SSLContext.load_verify_locations
tls_first tls.handshake_first
inbox_prefix inbox_prefix

Auth precedence mirrors the Go helper: user › creds › nkey › token. natsio does not allow combining mechanisms, so a token set alongside another mechanism is ignored (nats.go would send both).

Parsed but not applied

These fields are read and preserved on the returned Context (so you can use them yourself — e.g. the JetStream domain when creating a JetStream context), but natsio's connection layer has no equivalent and does not apply them:

Context field Context attribute Why not applied
jetstream_domain .jetstream_domain connection-independent; used at the JetStream API layer
jetstream_api_prefix .jetstream_api_prefix JetStream API layer
jetstream_event_prefix .jetstream_event_prefix JetStream API layer
socks_proxy .socks_proxy natsio has no custom-dialer / proxy hook
nsc .nsc would shell out to the nsc binary
user_jwt .user_jwt the Go helper parses but never applies it either
color_scheme .color_scheme CLI cosmetic
description .description metadata
windows_cert_store* .windows_cert_store unsupported — connect_kwargs() raises ContextError

Unrecognised JSON keys are kept in Context.raw and reported by Context.unknown_keys.

Interop with the nats CLI

This package only ever reads context files; it never writes them. Manage contexts with the CLI (nats context add|select|edit|ls|rm). Files written by this library's format are byte-compatible with what the CLI expects.

License

Apache-2.0.

Example

A runnable script is at examples/basic.py — start a server with just server, then:

python extensions/natsio-natscontext/examples/basic.py

Metadata

Release files for natsio-natscontext 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for natsio-natscontext 0.1.1
File Size Uploaded Provenance Actions
natsio_natscontext-0.1.1.tar.gz 7.1 kB With provenance Details for natsio_natscontext-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for natsio-natscontext 0.1.1
File Interpreter ABI Platform Actions
natsio_natscontext-0.1.1-py3-none-any.whl Python 3 none any Details for natsio_natscontext-0.1.1-py3-none-any.whl

Total release size: 15.0 kB

Release files / natsio_natscontext-0.1.1.tar.gz

Download URL natsio_natscontext-0.1.1.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d23393b42c6c767969230a3274d3679161851323913253efb5489d001de68e2d
BLAKE2b-256 checksum
How to use checksums
2211a98436bb703d48090ba592570c65252a071f4fb7721388e77886a664449a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / natsio_natscontext-0.1.1-py3-none-any.whl

Download URL natsio_natscontext-0.1.1-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ec62c798e4e519d95cd48e6af8abb5f21d5737343d4c09f7ded2f406195511fe
BLAKE2b-256 checksum
How to use checksums
c730374267355546285363cb5e1084a58b147c298a6f5985c0b4c89484eb9b3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Security for natsio-natscontext 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for natsio_natscontext-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page