Typescript SDK for commercetools platform API
Install
npm install --save @commercetools/platform-sdk
Browser
<script src="https://unpkg.com/@commercetools/platform-sdk/dist/platform-sdk.umd.js"></script>
<script>
</script>
Usage example
import { createAuthMiddlewareForClientCredentialsFlow } from '@commercetools/sdk-middleware-auth'
import { createHttpMiddleware } from '@commercetools/sdk-middleware-http'
import { createClient } from '@commercetools/sdk-client'
import {
createApiBuilderFromCtpClient,
ApiRoot,
} from '@commercetools/platform-sdk'
import fetch from 'node-fetch'
const projectKey = 'some_project_key'
const authMiddleware = createAuthMiddlewareForClientCredentialsFlow({
host: 'https://auth.europe-west1.gcp.commercetools.com',
projectKey,
credentials: {
clientId: 'some_id',
clientSecret: 'some_secret',
},
fetch,
})
const httpMiddleware = createHttpMiddleware({
host: 'https://api.europe-west1.gcp.commercetools.com',
fetch,
})
const ctpClient = createClient({
middlewares: [authMiddleware, httpMiddleware],
})
const apiRoot: 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 => {
})