Skip to content

Authentication

Describes github.com/stxapp/stx-go v0.3.1, the current release.

func LoadPrivateKey(value string) (ed25519.PrivateKey, error)

LoadPrivateKey parses a PKCS#8 Ed25519 private key from PEM text or from a path to a PEM file (a leading ~ is expanded).

func ParsePrivateKeyPEM(data []byte) (ed25519.PrivateKey, error)

ParsePrivateKeyPEM parses a PKCS#8 Ed25519 private key from PEM bytes.

func SigningMessage(timestampMs int64, method, path string) string

SigningMessage returns the string an API key signs: the Unix time in milliseconds, the uppercase HTTP method and the request path (including any query string), concatenated without separators.

type APIKey struct {
// contains filtered or unexported fields
}

APIKey is an STX API key: the key id sent with every request, and an Ed25519 signer. The private key never leaves the process; only signatures are sent. It implements Auth.

func NewAPIKey(id, privateKey string) (*APIKey, error)

NewAPIKey builds an API key from its id and an Ed25519 private key given as PKCS#8 PEM text or as a path to a PEM file (a leading ~ is expanded).

func NewAPIKeyWithSigner(id string, signer crypto.Signer) (*APIKey, error)

NewAPIKeyWithSigner builds an API key around any crypto.Signer that produces pure Ed25519 signatures, for keys held in an HSM or a cloud KMS. An ed25519.PrivateKey works as is.

func (k *APIKey) AuthHeaders(_ context.Context, method, path string) (http.Header, error)

AuthHeaders signs one HTTP request.

func (k APIKey) GoString() string

GoString is what %#v prints: the key id, never key material.

func (k *APIKey) ID() string

ID returns the key id.

func (k *APIKey) Kind() string

Kind returns “api_key”.

func (k *APIKey) Sign(message []byte) ([]byte, error)

Sign returns the raw Ed25519 signature over message.

func (k *APIKey) SignedHeaders(method, path string, at time.Time) (http.Header, error)

SignedHeaders returns the three access headers for method and path at the given time. The server rejects a timestamp more than 30 seconds from its clock, so sign every request afresh.

func (k APIKey) String() string

String returns a description that never includes key material.

func (k *APIKey) WebSocketHeaders(_ context.Context, _ bool) (http.Header, error)

WebSocketHeaders signs the WebSocket handshake: GET and the socket path, without the query string.

type Auth interface {
// Kind is a stable label for logs, such as "api_key" or "oauth".
Kind() string
// AuthHeaders returns the headers for one HTTP request. It is called
// afresh for every attempt. path includes the query string, exactly as
// sent.
AuthHeaders(ctx context.Context, method, path string) (http.Header, error)
}

Auth authenticates requests. *APIKey (Ed25519 request signing) is the usual one; the oauth package provides bearer-token implementations for apps acting on a member’s behalf. Pass one with WithAuth.

type Reauthenticator interface {
Reauthenticate(ctx context.Context) (bool, error)
}

Reauthenticator is an Auth that can recover from a 401, for example by refreshing an OAuth access token. After a 401 the client calls Reauthenticate once; when it returns true the request is sent again.

type WebSocketAuth interface {
WebSocketHeaders(ctx context.Context, reconnect bool) (http.Header, error)
}

WebSocketAuth is an Auth that can authenticate the WebSocket handshake. reconnect is true when the handshake follows a dropped connection.

STX is operated by XV Exchange, LLC, a Designated Contract Market regulated by the U.S. Commodity Futures Trading Commission (CFTC). Contracts traded on STX are cleared by XV Clearing, LLC, a CFTC-registered Derivatives Clearing Organization. Trading involves substantial risk of loss and may not be appropriate for everyone. Members risk losing the cost to enter any transaction, including fees. Consider whether trading is appropriate for you in light of your experience and financial resources. Nothing on this site is investment advice or a recommendation to trade. Past performance is not indicative of future results.

© 2026 STX. All rights reserved.llms.txtllms-full.txtView as Markdown
Docs version v1.10.5