Skip to main content

Overview

The SnapPay Go SDK provides a comprehensive, idiomatic interface for integrating payment processing, subscription management, and real-time event streaming into your Go applications. Built with modern Go patterns including context support, channel-based concurrency, and explicit error handling, it offers seamless integration with SnapPay’s platform. Requirements: Go 1.21+

Key Features

  • Idiomatic Go design with context.Context support for cancellation and timeouts
  • Channel-based concurrency for optimal performance and real-time event handling
  • Explicit error handling following Go conventions
  • Full type safety with struct definitions and interface contracts
  • Goroutine-safe client for concurrent usage across your application
  • Real-time event streaming via Server-Sent Events (SSE) using channels
  • Context-aware operations with deadline propagation and cancellation
  • Environment-based configuration with sensible defaults

Installation

Add to your go.mod:

Configuration & Initialization

The SDK client is configured via a constructor function that accepts an optional configuration struct. All operations support context.Context for proper timeout and cancellation handling.

API Key Authentication

Authentication is handled via an API key that must start with pk_test_ (for testing) or pk_live_ (for production). Configure your API key in one of these ways: Environment Variable (Recommended):
Direct Configuration:

Core Methods

All methods accept a context.Context as the first parameter and return a result struct pointer and an error, following standard Go patterns. The client is safe for concurrent use across multiple goroutines.

Customer Management

GetCustomer

Retrieves or creates a customer record (upsert logic).
Parameters:
  • CustomerId (string): Customer identifier
  • Email (*string): Customer email address (optional)
  • Name (*string): Customer full name (optional)
Returns: (*Customer, error)

Checkout Sessions

CreateCheckoutSession

Creates a payment checkout session URL for customer purchases.
Parameters:
  • CustomerID (string): SnapPay customer ID
  • ProductID (string): Product ID from your snappay dashboard
  • PriceID (*string): Price ID from your snappay dashboard (optional)
  • SuccessURL (string): URL to redirect after successful payment
  • CancelURL (*string): URL to redirect on cancellation (optional)
Returns: (*CheckoutSession, error)

Access Control

CheckAccess

Checks if a customer has access to a specific feature based on their subscription and usage limits.
Parameters:
  • CustomerID (string): SnapPay customer ID
  • FeatureID (string): Feature identifier
Returns: (*AccessCheck, error)

Usage Tracking

TrackUsage

Reports usage for a metered feature. This action is idempotent to prevent duplicate tracking.
Parameters:
  • CustomerID (string): SnapPay customer ID
  • FeatureID (string): Feature identifier for usage tracking
  • Usage (int64): Usage amount to track
  • IdempotencyKey (*string): Prevents duplicate tracking (optional)
Returns: (*TrackUsageResponse, error)

GetUsage

Retrieves current usage details for a customer’s feature.
Parameters:
  • CustomerID (string): SnapPay customer ID
  • FeatureID (string): Feature identifier
Returns: (*[]GetUsageResponse, error)

Real-time Event Handling

Say goodbye to webhook hell! snappay eliminates the complexity of managing payment webhooks by processing all provider webhooks (Stripe, PayPal, etc.) internally and delivering clean, structured events directly to your application via Server-Sent Events (SSE) using Go channels.

🚫 What You DON’T Need:

  • No webhook endpoints to create and maintain
  • No webhook signature verification
  • No webhook retry logic or failure handling
  • No webhook security concerns
  • No debugging webhook delivery issues

✅ What You GET:

  • Real-time events delivered instantly via Go channels
  • Guaranteed delivery with automatic reconnection
  • Clean, structured data - no raw webhook payloads
  • Channel-based concurrency perfect for Go’s goroutine model
  • Context-aware with proper cancellation support

Supported Events

See the list of supported events: Server-sent Event Types

Stream Events (Async Generator)

Consume the SSE stream via channels for direct, context-aware processing:

Event Handlers

Register handlers as separate functions and wire them up succinctly:

Error Handling

Error Types