Suger

TypeScript SDK

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

Step 1: Install the SDK

To include the Suger SDK in your project, you can use npm in your terminal:

npm install suger-sdk-ts --save

Step 2: Get an OAuth access token

The Suger API uses the OAuth 2.0 client-credentials flow. First, create an 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:

// Exchange OAuth App credentials for a bearer token. Cache it in memory and
// request a new one when it expires (default lifetime: 1 hour).
async function getAccessToken(clientId: string, clientSecret: string): Promise<string> {
    const res = await fetch("https://apiv2.suger.cloud/oauth2/token", {
        method: "POST",
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        body: new URLSearchParams({
            grant_type: "client_credentials",
            client_id: clientId,
            client_secret: clientSecret,
            resource: "https://api.suger.cloud",
        }),
    })
    const { access_token } = await res.json()
    return access_token
}

The resource=https://api.suger.cloud parameter is required — it scopes the issued JWT to the Suger API. See API Access for the full token exchange, caching, and rotation details.

Step 3: Create Config for Api

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

const accessToken = await getAccessToken("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
const server = new ServerConfiguration<{}>("https://api.suger.cloud", {})
const authConfig: api.AuthMethodsConfiguration = {
    APIKeyAuth: "Bearer " + accessToken,
}

// Create configuration parameter object
const configurationParameters = {
    baseServer: server, // First server is default
    authMethods: authConfig,
}
const configuration = new Configuration(configurationParameters)

Step 4: Create Api and Call APIs

Now you can use the configuration object to create an API instance and access Suger services:

const offerApi = new OfferApi(configuration);
const orgId = 'your_org_id'
const offerId = "your_offer_id"
const offer = await offerApi.getOffer(orgId, offerId)

Legacy authentication (API key)

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

const authConfig: api.AuthMethodsConfiguration = {
    APIKeyAuth: "Key " + "your-api-key",
}

Conclusion

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

Spotted something wrong or out of date on this page? Tell us and we'll correct it.