# Go SDK

Follow these steps to get started with the Suger Go SDK.

## Step 1: Install the SDK

To include the Suger SDK in your project, you can clone new release version of the SDK in your terminal:

```bash
git clone https://github.com/sugerio/suger-sdk-go.git
```

Put the package under your project folder and add the following in import:

```go
```

## Step 2: Get an OAuth access token

The Suger API uses the OAuth 2.0 client-credentials flow. First, create an
[OAuth App](https://doc.suger.io/get-started/oauth-app/) in your organization
settings to get a **Client ID** and **Client Secret**. Then exchange them for a
short-lived (1 hour) bearer token:

```go
	"encoding/json"
	"net/http"
	"net/url"
)

// getAccessToken exchanges OAuth App credentials for a bearer token.
// Cache the returned token in memory and request a new one when it expires.
func getAccessToken(clientID, clientSecret string) (string, error) {
	resp, err := http.PostForm("https://apiv2.suger.cloud/oauth2/token", url.Values{
		"grant_type":    {"client_credentials"},
		"client_id":     {clientID},
		"client_secret": {clientSecret},
		"resource":      {"https://api.suger.cloud"},
	})
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	var token struct {
		AccessToken string `json:"access_token"`
		ExpiresIn   int    `json:"expires_in"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&token); err != nil {
		return "", err
	}
	return token.AccessToken, nil
}
```

The `resource=https://api.suger.cloud` parameter is required — it scopes the
issued JWT to the Suger API. See [API Access](https://doc.suger.io/get-started/oauth-app/)
for the full token exchange, caching, and rotation details.

## Step 3: Set up your configuration and create the ApiClient

Create a configuration and attach the bearer token. `APIKeyAuth` writes the
`Authorization` header verbatim, so pass the OAuth token as `Bearer <token>`:

```go
configuration := suger.NewConfiguration()
configuration.Servers = suger.ServerConfigurations{
	{
		URL:         "https://api.suger.cloud",
		Description: "No description provided",
	},
}
apiClient := suger.NewAPIClient(configuration)

accessToken, err := getAccessToken("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
if err != nil {
	// handle error
}
auth := context.WithValue(
	context.Background(),
	suger.ContextAPIKeys,
	map[string]suger.APIKey{
		"APIKeyAuth": {Key: "Bearer " + accessToken},
	},
)
```

## Step 4: Use the Client

Now that you have your client set up, you can call Suger services. Here’s a simple example of how to make a service
call:

```go
var orgId = "your_org_id"
var offerId = "your_offer_id"
execute, response, err := apiClient.OfferAPI.GetOffer(auth, orgId, offerId).Execute()
```

## Legacy authentication (API key)

:::warning
API-key authentication is **deprecated**. New integrations should use the OAuth
access token shown above. The API-key example below is retained only for
existing integrations.
:::

To authenticate an existing integration with an API key, pass it as `Key <key>`
instead of a bearer token:

```go
auth := context.WithValue(
	context.Background(),
	suger.ContextAPIKeys,
	map[string]suger.APIKey{
		"APIKeyAuth": {Key: "Key " + "your_api_key"},
	},
)
```

## Conclusion

You are now ready to use the Suger Go SDK in your application! For more detailed information and advanced usage, please
refer to the official documentation.
