Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@commercetools/sdk-client-v2

commercetools399.2kMIT3.0.0TypeScript support: included

commercetools Composable Commerce TypeScript SDK client.

commercetools, composable commerce, sdk, typescript, client, middleware, http, oauth, auth

readme

Commercetools Composable Commerce TypeScript SDK client

Usage examples

Browser environment

<script src="https://unpkg.com/@commercetools/sdk-client-v2@latest/dist/commercetools-sdk-client-v2.umd.js"></script>
<script src="https://unpkg.com/@commercetools/platform-sdk@latest/dist/commercetools-platform-sdk.umd.js"></script>
<script>
  // global: @commercetools/sdk-client-v2
  // global: @commercetools/platform-sdk
  ;(function () {
    //  We can now access the sdk-client-v2 and platform-sdk object as:
    //  const { ClientBuilder } = this['@commercetools/sdk-client-v2']
    //  const { createApiBuilderFromCtpClient } = this['@commercetools/platform-sdk']
    //  or
    //  const { ClientBuilder } = window['@commercetools/sdk-client-v2']
    //  const { createApiBuilderFromCtpClient } = window['@commercetools/platform-sdk']
  })()
</script>

See full usage example here

Node environment

npm install --save @commercetools/sdk-client-v2
npm install --save @commercetools/platform-sdk
const {
  ClientBuilder,
  createAuthForClientCredentialsFlow,
  createHttpClient,
} = require('@commercetools/sdk-client-v2')
const { createApiBuilderFromCtpClient } = require('@commercetools/platform-sdk')

const projectKey = 'mc-project-key'
const authMiddlewareOptions = {
  host: 'https://auth.europe-west1.gcp.commercetools.com',
  projectKey,
  credentials: {
    clientId: 'mc-client-id',
    clientSecret: 'mc-client-secrets',
  },
  oauthUri: '/oauth/token', // - optional: custom oauthUri
  scopes: [`manage_project:${projectKey}`],
  fetch,
}

const httpMiddlewareOptions = {
  host: 'https://api.europe-west1.gcp.commercetools.com',
  fetch,
}

const client = new ClientBuilder()
  .withProjectKey(projectKey)
  .withMiddleware(createAuthForClientCredentialsFlow(authMiddlewareOptions))
  .withMiddleware(createHttpClient(httpMiddlewareOptions))
  .withUserAgentMiddleware()
  .build()

// or
const client = new ClientBuilder()
  .withProjectKey(projectKey)
  .withClientCredentialsFlow(authMiddlewareOptions)
  .withHttpMiddleware(httpMiddlewareOptions)
  .withUserAgentMiddleware()
  .build()

const apiRoot = createApiBuilderFromCtpClient(client)

// calling the Composable Commerce functions
// get project details
apiRoot
  .withProjectKey({
    projectKey,
  })
  .get()
  .execute()
  .then((x) => {
    /*...*/
  })

// create a productType
apiRoot
  .withProjectKey({ projectKey })
  .productTypes()
  .post({
    body: { name: 'product-type-name', description: 'some description' },
  })
  .execute()
  .then((x) => {
    /*...*/
  })

// create a product
apiRoot
  .withProjectKey({ projectKey })
  .products()
  .post({
    body: {
      name: { en: 'our-great-product-name' },
      productType: {
        typeId: 'product-type',
        id: 'some-product-type-id',
      },
      slug: { en: 'some-slug' },
    },
  })
  .execute()
  .then((x) => {
    /*...*/
  })

// -----------------------------------------------------------------------
// The sdk-client-v2 also has support for the old syntax
import {
  createClient,
  createHttpClient,
  createAuthForClientCredentialsFlow,
} from '@commercetools/sdk-client-v2'
import { createApiBuilderFromCtpClient } from '@commercetools/platform-sdk'

const projectKey = 'some_project_key'

const authMiddleware = createAuthForClientCredentialsFlow({
  host: 'https://auth.europe-west1.gcp.commercetools.com',
  projectKey,
  credentials: {
    clientId: 'some_id',
    clientSecret: 'some_secret',
  },
  fetch,
})

const httpMiddleware = createHttpClient({
  host: 'https://api.europe-west1.gcp.commercetools.com',
  fetch,
})

const ctpClient = createClient({
  middlewares: [authMiddleware, httpMiddleware],
})

const apiRoot = createApiBuilderFromCtpClient(ctpClient)

apiRoot
  .withProjectKey({
    projectKey,
  })
  .get()
  .execute()
  .then((x) => {
    /*...*/
  })

apiRoot
  .withProjectKey({ projectKey })
  .productTypes()
  .post({
    body: { name: 'product-type-name', description: 'some description' },
  })
  .execute()
  .then((x) => {
    /*...*/
  })

apiRoot
  .withProjectKey({ projectKey })
  .products()
  .post({
    body: {
      name: { en: 'our-great-product-name' },
      productType: {
        typeId: 'product-type',
        id: 'some-product-type-id',
      },
      slug: { en: 'some-slug' },
    },
  })
  .execute()
  .then((x) => {
    /*...*/
  })

See full usage example here

changelog

@commercetools/sdk-client-v2

3.0.0

Major Changes

2.5.0

Minor Changes

  • #676 8b1aecc Thanks @ajimae! - Add custom logger function

  • #679 b8bc24d Thanks @ajimae! - - Add a before middleware function that runs before the coco api call is initiated

    • Add an after middleware function that runs after the coco api call is done executing

2.4.1

Patch Changes

  • #671 344fd2d Thanks @ajimae! - Fix malformed uri error due to wrong object to uri serialization.

2.4.0

Minor Changes

  • #637 a000d70 Thanks @ajimae! - - add functionality to override limit value in process function
    • add sort options to process function to override default value

2.3.0

Minor Changes

  • #564 db69db1 Thanks @ajimae! - - remove newrelic as dependency in @commercetools/ts-sdk-apm package
    • add useragent key with a default typescript-sdk-apm-middleware string
    • add example express app on how to add dynatrace monitoring in the ts sdk

2.2.3

Patch Changes

2.2.2

Patch Changes

  • #545 a6d0df2 Thanks @ajimae! - - fix user-agent header
    • remove User-Agent key from the middleware request header

2.2.1

Patch Changes

  • #548 510838c Thanks @ajimae! - add headersWithStringBody option to httpMiddlewareOptions

2.2.0

Minor Changes

  • #458 c2bf0a5 Thanks @ajimae! - Introduce (newrelic) application performance monitoring

  • #458 c2bf0a5 Thanks @ajimae! - - add newrelic APM (application performance monitoring) to the sdk

    • add withTelemetryMiddleware() middleware creator method to ClientBuilder class
    • add basic newrelic application configuration module
    • add examples to repository example folder to demonstrate how this feature can be used

2.1.6

Patch Changes

  • #453 a83e653 Thanks @ajimae! - - Remove all querystring dependency
    • Add qs dependency to requiring package
    • Refactor code to accommodate new implementation

2.1.5

Patch Changes

  • #438 057e260 Thanks @upperwalker! - - Handle node-fetch text parse error in sdk client http middleware
    • Adds catch block to prevent unhandled promise rejection on parsing response text

2.1.4

Patch Changes

2.1.3

Patch Changes

2.1.2

Patch Changes

  • #415 caca661 Thanks @ajimae! - fix uri notfound error for 404 error when includeRequestInErrorResponse is set to false.

2.1.1

Patch Changes

2.1.0

Minor Changes

Patch Changes

2.0.2

Patch Changes

  • #355 0db2947 Thanks @ajimae! - fix SDK to accept custom tokenCache store allow anonymous auth flow to accept custom tokenCache store fix refresh token flow and password flow to accept custom tokenCache store also

2.0.1

Patch Changes

  • #347 f0e84dd Thanks @github-actions! - Update generated SDKs

    Add quotes() method for the My Quote endpoint

    Usage:

    request: apiRoot
      .withProjectKey({ projectKey: 'test_projectKey' })
      .me()
      .quotes()
      .withId({ ID: 'test_ID' })
      .get({ queryArgs: { expand: 'expand' } }),

    Add ByProjectKeyMeQuotesByIDRequestBuilder class for quotes-request model

    Add ByProjectKeyMeQuotesKeyByKeyRequestBuilder class for quotes-request model

    Add ByProjectKeyMeQuotesRequestBuilder class for quotes-request model

2.0.0

Major Changes

1.4.2

Patch Changes

  • #331 007d9c6 Thanks @ajimae! - - Fix issues with buffer request body always being converted to json (stringified)
    • Abandon the static Buffer.isBuffer() method in favour of custom isBuffer function

1.4.1

Patch Changes

1.4.0

Minor Changes

Patch Changes

  • #248 7512c3f Thanks @ajimae! - - add an option (includeRequestInErrorResponse) to include or exclude original request from error responses.

1.3.0

Minor Changes

  • #241 85f5be3 Thanks @ajimae! - Releasing the TS SDK with the following changelogs

    • added functionalities to extend client user agent
    • custom field added to OrderFromCardDraft

1.2.0

Minor Changes

1.1.0

Minor Changes

Patch Changes

1.0.2

Patch Changes

1.0.1

Patch Changes

1.0.0

Major Changes

0.2.0

Minor Changes

0.1.2

Patch Changes

0.1.1

Patch Changes

0.1.0

Minor Changes