7.18.0 • Published 10 months ago

@commercetools/platform-sdk v7.18.0

Weekly downloads
3,498
License
MIT
Repository
github
Last release
10 months ago

TypeScript SDK for commercetools Composable Commerce HTTP API

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 fetch = require('node-fetch')

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 platform 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'
import fetch from 'node-fetch'

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

@labdigital/commercetools-node-mock@commercetools/client-sdk@frontastic/extensions@everything-registry/sub-chunk-190nest-commercetools@commercetools-test-data/category@commercetools-test-data/cent-precision-money@commercetools-test-data/channel@commercetools-test-data/associate-role@commercetools-test-data/attribute@commercetools-test-data/attribute-definition@commercetools-test-data/commons@commercetools-test-data/custom-application@commercetools-test-data/cart@commercetools-test-data/cart-discount@commercetools-test-data/cart-discount-value-absolute@commercetools-test-data/cart-discount-value-fixed@commercetools-test-data/cart-discount-value-gift-line-item@commercetools-test-data/cart-discount-value-relative@commercetools-test-data/customer-group@commercetools-test-data/customers-search-list-my-view@commercetools-test-data/discount-code@commercetools-test-data/custom-object@commercetools-test-data/custom-view@commercetools-test-data/customer@commercetools-test-data/attribute-group@commercetools-test-data/attribute-plain-enum-value@commercetools-test-data/attribute-type@commercetools-test-data/business-unit@commercetools-test-data/state@commercetools-test-data/store@commercetools-test-data/tax-category@commercetools-test-data/tax-rate@commercetools-test-data/order@commercetools-test-data/organization@commercetools-test-data/payment@commercetools-test-data/platform-limits@commercetools-test-data/price@commercetools-test-data/shipping-method@commercetools-test-data/shopping-list@commercetools-test-data/staged-quote@commercetools-test-data/standalone-price@commercetools-test-data/product@commercetools-test-data/product-catalog-data@commercetools-test-data/product-data@commercetools-test-data/product-discount@commercetools-test-data/product-discount-value-absolute@commercetools-test-data/product-discount-value-external@commercetools-test-data/product-discount-value-relative@commercetools-test-data/product-projection@commercetools-test-data/product-selection@commercetools-test-data/product-type@commercetools-test-data/product-variant@commercetools-test-data/project@commercetools-test-data/type@commercetools-test-data/user@commercetools-test-data/image@commercetools-test-data/inventory-entry@commercetools-test-data/line-item@commercetools-test-data/money@commercetools-test-data/quote@commercetools-test-data/quote-request@commercetools-test-data/review@commercetools-test-data/zone@commercetools/connect-payments-sdk@devgurusio/nest-commercetools@cboyke/demotools@dc_commerce_innovacion/common@deptdash/commercetools@infinitebrahmanuniverse/nolb-_comm@krishtechnolabs/commercetools-strapi-connector@jonssonworkwear/strapi-plugin-commercetools-integration@qavajs/steps-ctp@plasmicpkgs/commercetools@plasmicpkgs/commerce-commercetools@peepoglad/commercetools-messagescofe-ct-b2b-ecommercecofe-ct-ecommercecommercetools-strapi-integrationdash-commercetoolsct-common-toolsct-external-connectionsct-m-js
7.18.0

10 months ago

7.17.0

10 months ago

7.15.0

10 months ago

7.16.0

10 months ago

7.13.0

11 months ago

7.14.0

11 months ago

7.12.0

11 months ago

7.11.0

12 months ago

7.9.0

1 year ago

7.10.0

1 year ago

7.8.0

1 year ago

7.7.0

1 year ago

7.6.0

1 year ago

7.5.0

1 year ago

7.4.0

1 year ago

7.3.0

1 year ago

7.2.0

1 year ago

7.2.0-alpha.4

2 years ago

7.2.0-alpha.3

2 years ago

7.2.0-alpha.2

2 years ago

7.2.0-alpha.1

2 years ago

7.1.0

2 years ago

4.10.1

2 years ago

5.0.0

2 years ago

6.0.0

2 years ago

7.0.0

2 years ago

4.11.0

2 years ago

4.10.0

2 years ago

4.9.0

2 years ago

4.7.1

2 years ago

4.8.1

2 years ago

4.8.0

2 years ago

4.7.0

2 years ago

4.6.0

2 years ago

4.5.0

3 years ago

4.4.0

3 years ago

4.1.0

3 years ago

4.3.0

3 years ago

4.2.0

3 years ago

4.0.0

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

2.8.0

3 years ago

3.0.0

3 years ago

2.7.0

3 years ago

2.6.0

3 years ago

2.5.0

3 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.4.1

4 years ago

2.4.0

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.20.0

4 years ago

1.19.0

4 years ago

1.18.1

4 years ago

1.18.0

4 years ago

1.17.1

4 years ago

1.17.0

4 years ago

1.16.0

4 years ago

1.15.0

4 years ago

1.14.0

4 years ago

1.13.0

4 years ago

1.12.0

4 years ago

1.10.0

4 years ago

1.9.1

4 years ago

1.9.0

4 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago