Skip to content
Skip to content
PGB Learn

The Go and Rust SDKs

Go

go get github.com/PGBHub/pgb-sdk-go
client := pgbhub.NewClient(pgbhub.Config{
	APIKey:    "your_api_key",
	ProjectID: "my-project",
})

res, err := client.InvokeAction(ctx, "approveOrder", map[string]any{"orderId": "ord_102"}, false)
if err != nil {
	log.Fatalf("Action failed: %v", err)
}
fmt.Printf("Status: %s (took %.2f ms)\n", res.Status, res.DurationMS)

Everything in Config can come from PGBHUB_API_KEY, PGBHUB_PROJECT_ID or PGBHUB_ENDPOINT. Timeout defaults to 30 seconds and MaxRetries to 2, for reads only.

Auth and mail:

login, _ := client.Auth.SignIn(ctx, "[email protected]", "a-long-password")
if login.MFARequired {
	client.Auth.VerifyMFA(ctx, login.ChallengeToken, "123456")
}

res, err := client.Mail.Send(ctx, pgbhub.SendMailOptions{
	To: "[email protected]", Subject: "Your receipt", Text: "Thanks for your order.",
})
if pgbhub.IsMailProviderUnavailable(err) {
	// sending is temporarily unavailable, retry later
}

Errors are *pgbhub.APIError with StatusCode, Code, RetryAfter and the raw Body:

var apiErr *pgbhub.APIError
if errors.As(err, &apiErr) && apiErr.StatusCode == 429 {
	time.Sleep(apiErr.RetryAfter)
}

Storage, with the secret key:

client.Storage.CreateBucket(ctx, "receipts", &pgbhub.CreateBucketOptions{Visibility: pgbhub.BucketPrivate})
link, err := client.Storage.CreateSignedURL(ctx, "receipts", "invoices/inv-1.pdf",
	&pgbhub.SignedURLOptions{ExpiresIn: 15 * time.Minute})

Rust

[dependencies]
pgbhub = { git = "https://github.com/PGBHub/pgb-sdk-rs" }
tokio = { version = "1", features = ["full"] }
serde_json = "1"
use pgbhub::{PGBHubClient, PGBHubConfig};
use std::collections::HashMap;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = PGBHubClient::new(PGBHubConfig {
        api_key: Some("your_api_key".to_string()),
        project_id: Some("my-project".to_string()),
        ..PGBHubConfig::default()
    });

    let mut params = HashMap::new();
    params.insert("orderId".to_string(), serde_json::json!("ord_102"));

    let res = client.invoke_action("approveOrder", params, false).await?;
    println!("Status: {} (took {:.2} ms)", res.status, res.duration_ms);
    Ok(())
}

PGBHubConfig::default() reads the environment. Errors are PGBHubError::Api { status, message, code, retry_after }, and err.status() and err.code() read them without a match.

let login = client.auth.sign_in("[email protected]", Some("a-long-password")).await?;
if login.mfa_required {
    client.auth.verify_mfa(&login.challenge_token.unwrap(), "123456").await?;
}

What both share

  • Reads retry on 502, 503, 504 and connection errors. Writes never retry.
  • Auth, actions with effects, mail, and storage with signed links are covered.
  • The secret key is needed for mail and storage administration.

Try it

  1. Install the SDK for your language.
  2. Create an action in the Console (Actions > + Define New Action) and call it with dry_run set to true.
  3. Trigger a deliberate error, such as a wrong key, and print its status.

Recap

  • Go and Rust start with InvokeAction and invoke_action.
  • Configure through struct fields or environment variables.
  • Errors carry the status, code and retry delay for rate limits.

Open Access Course

Free

forever
  • 7 structured lessons across 4 modules
  • Interactive knowledge checks with instant feedback
  • Verifiable completion certificate & PDF