Go library for managing Betfair API session tokens with cloud-agnostic blob storage (Go Cloud).
Persists the session token, resumes it when still valid, and logs in again when missing or expired. Swap storage backends (local disk, Azure, AWS, GCP) without changing application code.
- Go 1.25.0+
- betfair-go for Betfair API login, resume, and logout (installed automatically as a dependency)
- A valid Betfair account with certificate login enabled
- Betfair application key
- Client certificate and private key (PEM)
- A
gocloud.dev/blobbucket (local directory for dev, Azure Blob / S3 / GCS in production)
Install:
go get github.com/LonecastSystems/betfair-sessionImport the package:
import (
"context"
"github.com/LonecastSystems/betfair-go"
"github.com/LonecastSystems/betfair-session"
"gocloud.dev/blob/fileblob"
)Basic flow:
bucket, err := fileblob.OpenBucket("./session-data", &fileblob.Options{
NoTempDir: true, // required on Windows if the bucket is not on the same drive as %TEMP%
})
if err != nil {
// handle error
}
defer bucket.Close()
tlsConfig, err := betfair.GetTLSConfigFromBytes(certPEM, keyPEM)
if err != nil {
// handle error
}
client := betfair.NewClient(&betfair.ClientConfig{
Tls: tlsConfig,
ApplicationKey: appKey,
})
mgr := session.NewSessionManager(client, username, password)
token, err := mgr.GetSession(context.Background(), bucket, "session.token")
if err != nil {
// handle error
}
_ = tokenGetSession is idempotent: you can call it before every API request (or on a schedule) without creating a new login each time. If a stored token is still valid, it is resumed and returned; a new login only runs when the token is missing, empty, or no longer accepted by Betfair.
This follows Betfair’s recommended session management:
- Use one session across multiple API calls and goroutines — do not log in on every request.
- When a session expires, obtain a new session token via login (or resume when the stored token is still valid).
- Handle
INVALID_SESSION_INFORMATIONin your application: callGetSessionagain (or retry the API call after refreshing the session) when Betfair returns that error.
Betfair sessions last up to 12 hours by default; the Keep Alive API can extend an active session. Excessive logins can trigger a 20-minute login lockout — reusing a valid session avoids that.
- Checks that the bucket is accessible.
- If
sessionKeyexists in the bucket, reads the token and callsResumeon the Betfair client. - If resume fails or the object is missing, calls
Loginand writes the new token withWriteAll. - If writing the token fails after login, calls
Logouton the client.
Storage is cloud-agnostic: any Go Cloud blob URL works, for example:
| Environment | Example |
|---|---|
| Local dev | fileblob.OpenBucket(dir, &fileblob.Options{NoTempDir: true}) |
| Azure | azblob://mycontainer (via blob.OpenBucket) |
| AWS | s3://mybucket |
| GCP | gs://mybucket |
Store only the session token in the bucket. Keep username, password, and certificates in a secrets manager (e.g. Azure Key Vault), not in blob storage.
- Keep credentials, application keys, and certificate paths out of source code. Read them from environment variables or your secrets manager.
- On Windows, if the bucket path is on a different drive than
%TEMP%, setfileblob.Options.NoTempDir: trueor the final rename on write can fail. - Call
GetSessionbefore API use (or when you seeINVALID_SESSION_INFORMATION); the token is cached on thebetfair.ClientafterResume/Login. - Restrict bucket access: the session token is as sensitive as login credentials for API purposes.
- Depends on betfair-go for login, resume, and logout.