7.8.0 • Published 2 days ago

@commercetools/platform-sdk v7.8.0

Weekly downloads
3,498
License
MIT
Repository
github
Last release
2 days 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@krishtechnolabs/commercetools-strapi-connector@peepoglad/commercetools-messages@plasmicpkgs/commerce-commercetools@plasmicpkgs/commercetools@infinitebrahmanuniverse/nolb-_comm@qavajs/steps-ctp@jonssonworkwear/strapi-plugin-commercetools-integration@cboyke/demotools@commercetools-test-data/associate-role@commercetools-test-data/attribute@commercetools-test-data/attribute-definition@commercetools-test-data/attribute-plain-enum-value@commercetools-test-data/attribute-type@commercetools-test-data/business-unit@commercetools-test-data/zone@commercetools-test-data/tax-category@commercetools-test-data/tax-rate@commercetools-test-data/type@commercetools-test-data/user@commercetools-test-data/product-type@commercetools-test-data/product-variant@commercetools-test-data/project@commercetools-test-data/quote-request@commercetools-test-data/review@commercetools-test-data/shipping-method@commercetools-test-data/product-discount-value-external@commercetools-test-data/product-discount-value-relative@commercetools-test-data/product-selection@commercetools-test-data/order@commercetools-test-data/organization@commercetools-test-data/product@commercetools-test-data/product-catalog-data@commercetools-test-data/product-data@commercetools-test-data/shopping-list@commercetools-test-data/staged-quote@commercetools-test-data/standalone-price@commercetools-test-data/store@commercetools-test-data/state@commercetools-test-data/product-discount@commercetools-test-data/product-discount-value-absolute@commercetools-test-data/payment@commercetools-test-data/price@commercetools-test-data/quote@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/category@commercetools-test-data/cent-precision-money@commercetools-test-data/customer-group@commercetools-test-data/discount-code@commercetools-test-data/inventory-entry@commercetools-test-data/line-item@commercetools-test-data/money@commercetools-test-data/custom-view@commercetools-test-data/customer@commercetools-test-data/custom-application@commercetools-test-data/custom-object@commercetools-test-data/channel@commercetools-test-data/commons@commercetools-test-data/image@commercetools/connect-payments-sdk@deptdash/commercetools@devgurusio/nest-commercetoolscommercetools-strapi-integrationcofe-ct-b2b-ecommercecofe-ct-ecommercect-external-connectionsct-m-jsdash-commercetools
7.8.0

2 days ago

7.7.0

21 days ago

7.6.0

1 month ago

7.5.0

1 month ago

7.4.0

2 months ago

7.3.0

3 months ago

7.2.0

3 months ago

7.2.0-alpha.4

3 months ago

7.2.0-alpha.3

3 months ago

7.2.0-alpha.2

3 months ago

7.2.0-alpha.1

3 months ago

7.1.0

5 months ago

4.10.1

10 months ago

5.0.0

9 months ago

6.0.0

8 months ago

7.0.0

6 months ago

4.11.0

10 months ago

4.10.0

11 months ago

4.9.0

12 months ago

4.7.1

1 year ago

4.8.1

12 months ago

4.8.0

12 months ago

4.7.0

1 year ago

4.6.0

1 year ago

4.5.0

1 year ago

4.4.0

1 year ago

4.1.0

2 years ago

4.3.0

1 year ago

4.2.0

1 year ago

4.0.0

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

2.8.0

2 years ago

3.0.0

2 years ago

2.7.0

2 years ago

2.6.0

2 years ago

2.5.0

2 years ago

2.3.0

2 years ago

2.2.0

2 years ago

2.4.1

2 years ago

2.4.0

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.20.0

2 years ago

1.19.0

3 years ago

1.18.1

3 years ago

1.18.0

3 years ago

1.17.1

3 years ago

1.17.0

3 years ago

1.16.0

3 years ago

1.15.0

3 years ago

1.14.0

3 years ago

1.13.0

3 years ago

1.12.0

3 years ago

1.10.0

3 years ago

1.9.1

3 years ago

1.9.0

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.0

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.4.0

4 years ago

1.3.0

4 years ago