Authentication

The SDK supports multiple authentication methods to connect to Salesforce.

OAuth 2.0 Refresh Token

The recommended method for production applications. Uses a refresh token to obtain access tokens automatically.

Go
client, err := salesforce.NewClient(
    salesforce.WithOAuthRefresh(
        "your_client_id",
        "your_client_secret",
        "your_refresh_token",
    ),
)
if err != nil {
    log.Fatal(err)
}

// Connect authenticates and obtains access token
err = client.Connect(ctx)
Tip: To obtain a refresh token, use the OAuth 2.0 Web Server Flow in your Connected App.

Username-Password Flow

Simple authentication using username and password. Suitable for server-to-server integrations.

Go
client, err := salesforce.NewClient(
    salesforce.WithCredentials(
        "your_client_id",
        "your_client_secret",
    ),
    salesforce.WithPasswordAuth(
        "username@example.com",
        "password",
        "security_token",
    ),
)

err = client.Connect(ctx)
Note: The password must be concatenated with the security token. Get your security token from Salesforce Setup → My Personal Information → Reset Security Token.

Direct Access Token

Use an existing access token directly. Useful when you manage tokens externally.

Go
client, err := salesforce.NewClient(
    salesforce.WithAccessToken(
        "your_access_token",
        "https://yourinstance.salesforce.com",
    ),
)

// No Connect() needed - set token directly
client.SetAccessToken(accessToken, instanceURL)

Sandbox Environment

Connect to a Salesforce sandbox instead of production:

Go
client, err := salesforce.NewClient(
    salesforce.WithOAuthRefresh(clientID, clientSecret, refreshToken),
    salesforce.WithSandbox(),  // Uses test.salesforce.com
)

Custom Domain (My Domain)

Connect using a Salesforce My Domain:

Go
client, err := salesforce.NewClient(
    salesforce.WithOAuthRefresh(clientID, clientSecret, refreshToken),
    salesforce.WithCustomDomain("mycompany"),
    // Uses https://mycompany.my.salesforce.com
)

Token Refresh

Manually refresh the access token when needed:

Go
// Refresh token programmatically
err := client.RefreshToken(ctx)
if err != nil {
    log.Println("Token refresh failed:", err)
}

// Get current token info
token := client.GetToken()
fmt.Println("Instance URL:", token.InstanceURL)
fmt.Println("Expires at:", token.ExpiresAt)

Connected App Setup

To use OAuth authentication, create a Connected App in Salesforce:

  1. Go to Setup → App Manager → New Connected App
  2. Enable OAuth Settings
  3. Add callback URL (for web flow)
  4. Select OAuth scopes: api, refresh_token, offline_access
  5. Save and note the Consumer Key (Client ID) and Consumer Secret