Generate JSON Web Tokens
This guide generates an RS256 key pair and signs a short-lived JSON Web Token (JWT). Run this code in a trusted backend environment so the private key never reaches a browser or client app.
Conduit also supports ES256 with P-256 keys and EdDSA with Ed25519 keys. The examples in this guide use RS256.
The examples use an empty payload and add these fields:
algidentifies theRS256signing algorithm.kididentifies the public key that Conduit uses to verify the signature.iatrecords when the signer created the token.explimits the token lifetime to 10 minutes.
A JWT is signed, not encrypted. Anyone with the token can decode its header and payload, so don’t include secrets in either part.
Generate a key pair
The run-once scripts generate a 2,048-bit RSA private key in PKCS #8 format and a public key in SubjectPublicKeyInfo (SPKI) format. Both files use Privacy-Enhanced Mail (PEM) encoding. They stop instead of replacing an existing key pair.
Save the script as generate-keys.mjs or generate-keys.sh.
Run the example:
Keep private_key.pem secret. In the Conduit app, register public_key.pem as an RS256 public key, bind it to a Nodes API key, and copy the generated Key ID. For the complete dashboard flow, read the JWT authentication overview.
Sign a token
Always sign JWTs on your server. Never expose the private key or signing code in a browser or client app. For client-side RPC requests or transaction submission, your server can mint a short-lived JWT and return it to the client, which can cache it until shortly before it expires and send it with the API key endpoint URL. Alternatively, your server can sign the JWT and make the RPC request.
Set KEY_ID to the Key ID shown in Conduit. Each example reads private_key.pem, adds the required protected header and time claims, then prints the signed token.
Save the example as generate-jwt.ts or generate-jwt.go.
Install the signing library and generate a token:
Generate tokens on demand, and use the shortest lifetime that works for your service. When a token approaches expiration, create a replacement before sending another request.
Next, make an authenticated RPC request with the token.