Client & Authentication
Construct the client once and reuse it across your application.
Basic construction
client := sendafrica.NewClient(os.Getenv("SENDAFRICA_API_KEY"))If the API key argument is empty, NewClient reads SENDAFRICA_API_KEY from the environment.
Options
client := sendafrica.NewClient(apiKey,
sendafrica.WithBaseURL("https://api.sendafrica.online/v1"),
sendafrica.WithMaxRetries(3),
sendafrica.WithUserAgent("my-app/1.0"),
sendafrica.WithWebhookSecret("whsec_..."),
)| Option | Description |
|---|---|
WithBaseURL(string) | Override the default https://api.sendafrica.online/v1 (e.g. for sandbox). |
WithHTTPClient(*http.Client) | Supply your own http.Client (timeouts, proxies). |
WithMaxRetries(int) | Retry count for transient failures. Default 3. |
WithBearerToken(string) | Authenticate with a JWT (bearer) instead of an API key. |
WithUserAgent(string) | Custom User-Agent header. |
WithWebhookSecret(string) | Secret used by Webhooks to verify signatures. |
Under the hood
Every request also sends an X-Request-Id and sets Accept: application/json. Responses are unwrapped from the standard {success, data, error, request_id, timestamp} envelope.
Bearer-token mode
Use it when acting on a dashboard session instead of an account API key:
login, err := client.Login(ctx, "dev@example.com", "s3cret")
authed := sendafrica.NewClient("", sendafrica.WithBearerToken(login.AccessToken))Keep credentials server-side
Never commit the API key or store it in client-side code. Use separate keys per environment and rotate immediately after exposure.
Last updated on