Getting Started

Get up and running with the Salesforce Go SDK in minutes.

Requirements

Installation

Install the SDK using Go modules:

Terminal
go get github.com/PramithaMJ/salesforce/v2

Quick Start

Here's a minimal example to connect and query Salesforce:

main.go
package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/PramithaMJ/salesforce/v2"
)

func main() {
    // Create a new client
    client, err := salesforce.NewClient(
        salesforce.WithOAuthRefresh(
            os.Getenv("SF_CLIENT_ID"),
            os.Getenv("SF_CLIENT_SECRET"),
            os.Getenv("SF_REFRESH_TOKEN"),
        ),
    )
    if err != nil {
        log.Fatal(err)
    }

    // Connect to Salesforce
    ctx := context.Background()
    if err := client.Connect(ctx); err != nil {
        log.Fatal(err)
    }

    fmt.Println("Connected to:", client.InstanceURL())

    // Query some data
    result, err := client.Query().Execute(ctx, 
        "SELECT Id, Name FROM Account LIMIT 5")
    if err != nil {
        log.Fatal(err)
    }

    for _, record := range result.Records {
        fmt.Printf("Account: %s\n", record.StringField("Name"))
    }
}

Environment Variables

Set up your Salesforce credentials as environment variables. Choose the appropriate method for your use case:

.env

# Method 1: OAuth 2.0 Refresh Token (Recommended)
SF_CLIENT_ID=your_connected_app_client_id
SF_CLIENT_SECRET=your_connected_app_client_secret
SF_REFRESH_TOKEN=your_refresh_token

# Method 2: Username-Password Flow
SF_CLIENT_ID=your_connected_app_client_id
SF_CLIENT_SECRET=your_connected_app_client_secret
SF_USERNAME=your_username@example.com
SF_PASSWORD=your_password
SF_SECURITY_TOKEN=your_security_token

# Method 3: Direct Access Token
SF_ACCESS_TOKEN=your_access_token
SF_INSTANCE_URL=https://your-instance.salesforce.com

# Optional: Environment Configuration
SF_SANDBOX=true                    # Use sandbox (test.salesforce.com)
SF_MY_DOMAIN=mycompany             # Custom My Domain
SF_API_VERSION=59.0                # API version to use

Configuration Options

The SDK supports various configuration options using the functional options pattern:

Go
client, err := salesforce.NewClient(
    // Authentication (choose one)
    salesforce.WithOAuthRefresh(clientID, clientSecret, refreshToken),
    // OR: salesforce.WithPasswordAuth(username, password, securityToken),
    // OR: salesforce.WithAccessToken(accessToken, instanceURL),

    // Environment
    salesforce.WithSandbox(),                    // Use sandbox
    salesforce.WithCustomDomain("mycompany"),    // My Domain
    salesforce.WithAPIVersion("59.0"),          // API version

    // HTTP Configuration
    salesforce.WithTimeout(60 * time.Second),   // Request timeout
    salesforce.WithMaxRetries(5),               // Retry attempts
    salesforce.WithHTTPClient(customClient),    // Custom HTTP client

    // Logging & Debug
    salesforce.WithLogger(logger),               // Custom logger
    salesforce.WithDebug(true),                  // Enable debug mode
)
Option Description Default
WithOAuthRefresh OAuth 2.0 refresh token flow -
WithPasswordAuth Username-password flow -
WithAccessToken Direct access token -
WithCredentials Set client ID and secret -
WithAPIVersion API version to use 59.0
WithTimeout HTTP request timeout 30s
WithMaxRetries Max retry attempts 3
WithSandbox Use sandbox environment Production
WithCustomDomain My Domain configuration -
WithHTTPClient Custom HTTP client Default
WithLogger Custom logger None
WithDebug Enable debug logging false
Next Step: Learn about Authentication Methods to choose the best option for your use case.