Gathering detailed insights and metrics for @commercetools/ts-client
Gathering detailed insights and metrics for @commercetools/ts-client
Gathering detailed insights and metrics for @commercetools/ts-client
Gathering detailed insights and metrics for @commercetools/ts-client
The e-commerce SDK from commercetools for JavaScript written in TypeScript.
npm install @commercetools/ts-client
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.4
Supply Chain
100
Quality
95.5
Maintenance
100
Vulnerability
100
License
@commercetools/platform-sdk@8.12.0
Updated on Jul 04, 2025
@commercetools/history-sdk@5.4.0
Updated on Jul 04, 2025
@commercetools/importapi-sdk@6.4.0
Updated on Jul 04, 2025
@commercetools/platform-sdk@8.11.0
Updated on Jun 06, 2025
@commercetools/ts-client@3.4.1
Updated on Jun 06, 2025
@commercetools/importapi-sdk@6.3.0
Updated on Jun 03, 2025
TypeScript (99.85%)
JavaScript (0.12%)
Makefile (0.03%)
Total Downloads
2,900,997
Last Day
1,118
Last Week
76,619
Last Month
315,929
Last Year
2,549,568
MIT License
56 Stars
1,111 Commits
27 Forks
41 Watchers
17 Branches
338 Contributors
Updated on Jul 04, 2025
Minified
Minified + Gzipped
Latest Version
3.4.1
Package Id
@commercetools/ts-client@3.4.1
Unpacked Size
320.05 kB
Size
75.59 kB
File Count
49
NPM Version
10.9.2
Node Version
22.15.0
Published on
Jun 06, 2025
Cumulative downloads
Total Downloads
Last Day
146.3%
1,118
Compared to previous day
Last Week
-0.8%
76,619
Compared to previous week
Last Month
18.1%
315,929
Compared to previous month
Last Year
628.3%
2,549,568
Compared to previous year
1
5
This is the new and improved Typescript SDK client.
1npm install --save @commercetools/ts-client 2npm install --save @commercetools/platform-sdk 3 4or 5 6yarn add @commercetools/ts-client 7yarn add @commercetools/platform-sdk
1import { 2 type Next, 3 type HttpMiddlewareOptions, 4 type AuthMiddlewareBaseOptions, 5 type ClientRequest, 6 type MiddlewareRequest, 7 type MiddlewareResponse, 8 type Client, 9 createHttpMiddleware, 10 createConcurrentModificationMiddleware, 11 createAuthMiddlewareForClientCredentialsFlow, 12 ClientBuilder, 13} from '@commercetools/ts-client' 14import { createApiBuilderFromCtpClient } from '@commercetools/platform-sdk' 15 16const projectKey = 'mc-project-key' 17const authMiddlewareOptions = { 18 host: 'https://auth.europe-west1.gcp.commercetools.com', 19 projectKey, 20 credentials: { 21 clientId: 'mc-client-id', 22 clientSecret: 'mc-client-secrets', 23 }, 24 oauthUri: '/oauth/token', // - optional: custom oauthUri 25 scopes: [`manage_project:${projectKey}`], 26 httpClient: fetch, 27} 28 29const httpMiddlewareOptions = { 30 host: 'https://api.europe-west1.gcp.commercetools.com', 31 httpClient: fetch, 32} 33 34const retryOptions = { 35 maxRetries: 3, 36 retryDelay: 200, 37 backoff: true, 38 retryCodes: [503], 39} 40 41const loggerFn = (response) => { 42 // log response object 43 console.log(response) 44} 45 46// custom middleware 47function middleware(options) { 48 return (next: Next) => 49 async (request: MiddlewareRequest): Promise<MiddlewareResponse> => { 50 const { response, ...rest } = request 51 52 // other actions can also be carried out here e.g logging, 53 // error handling, injecting custom headers to http requests etc. 54 console.log({ response, rest }) 55 return next({ ...request }) 56 } 57} 58 59const client: Client = new ClientBuilder() 60 .withPasswordFlow(authMiddlewareOptions) 61 .withLoggerMiddleware({ loggerFn }) 62 .withCorrelationIdMiddleware({ 63 generate: () => 'fake-correlation-id' + Math.floor(Math.random() + 2), 64 }) 65 .withHttpMiddleware(httpMiddlewareOptions) 66 .withMiddleware(middleware({})) // <<<------------------- add the custom middleware here 67 .build() 68 69const apiRoot = createApiBuilderFromCtpClient(client) 70 71// calling the Composable Commerce `api` functions 72// get project details 73apiRoot 74 .withProjectKey({ projectKey }) 75 .get() 76 .execute() 77 .then((x) => { 78 /*...*/ 79 }) 80 81// create a productType 82apiRoot 83 .withProjectKey({ projectKey }) 84 .productTypes() 85 .post({ 86 body: { name: 'product-type-name', description: 'some description' }, 87 }) 88 .execute() 89 .then((x) => { 90 /*...*/ 91 }) 92 93// create a product 94apiRoot 95 .withProjectKey({ projectKey }) 96 .products() 97 .post({ 98 body: { 99 name: { en: 'our-great-product-name' }, 100 productType: { 101 typeId: 'product-type', 102 id: 'some-product-type-id', 103 }, 104 slug: { en: 'some-slug' }, 105 }, 106 }) 107 .execute() 108 .then((x) => { 109 /*...*/ 110 })
To create a client, use the ClientBuilder
class. The ClientBuilder
class provides a fluent API to configure the client.
1const authMiddlewareOptions = { 2 credentials: { 3 clientId: 'xxx', 4 clientSecret: 'xxx', 5 }, 6 host: 'https://auth.europe-west1.gcp.commercetools.com', 7 projectKey: 'xxx', 8} 9 10const httpMiddlewareOptions = { 11 host: 'https://api.europe-west1.gcp.commercetools.com', 12 httpClient: fetch, 13} 14 15const client = new ClientBuilder() 16 .withHttpMiddleware(httpMiddlewareOptions) 17 .withConcurrentModificationMiddleware() 18 .withClientCredentialsFlow(authMiddlewareOptions) 19 .build()
The withMiddleware
method can be used to add middleware functions (both built-in and custom middleware) in an ordered fashion.
1// Example 2const authMiddlewareOptions = { 3 credentials: { 4 clientId: 'xxx', 5 clientSecret: 'xxx', 6 }, 7 host: 'https://auth.europe-west1.gcp.commercetools.com', 8 projectKey: 'xxx', 9} 10 11const httpMiddlewareOptions = { 12 host: 'https://api.europe-west1.gcp.commercetools.com', 13 httpClient: fetch, 14} 15 16const logger = () => { 17 return (next) => async (request) => { 18 // log request object 19 console.log('Request:', request) 20 const response = await next(request) 21 22 // log response object 23 console.log('Response', response) 24 return response 25 } 26} 27 28const client = new ClientBuilder() 29 .withMiddleware( 30 createAuthMiddlewareForClientCredentialsFlow(authMiddlewareOptions) 31 ) 32 .withMiddleware(createHttpMiddleware(httpMiddlewareOptions)) 33 .withMiddleware(createConcurrentModificationMiddleware()) 34 .withMiddleware(logger()) 35 .build()
This will add the middleware in an ordered fashion starting with the:
Note that when using the withMiddleware
function to add a custom middleware along side other in built middleware functions, it will add the custom middleware to the start of the execution chain.
1// Example 2const authMiddlewareOptions = { 3 credentials: { 4 clientId: 'xxx', 5 clientSecret: 'xxx', 6 }, 7 host: 'https://auth.europe-west1.gcp.commercetools.com', 8 projectKey: 'xxx', 9} 10 11const httpMiddlewareOptions = { 12 host: 'https://api.europe-west1.gcp.commercetools.com', 13 httpClient: fetch, 14} 15 16const logger = () => { 17 return (next) => async (request) => { 18 // log request object 19 console.log('Request:', request) 20 const response = await next(request) 21 22 // log response object 23 console.log('Response', response) 24 return response 25 } 26} 27 28const client = new ClientBuilder() 29 .withClientCredentialsFlow(authMiddlewareOptions) 30 .withHttpMiddleware(httpMiddlewareOptions) 31 .withConcurrentModificationMiddleware() 32 .withMiddleware(logger()) 33 .build()
The order of execution is as follows:
No vulnerabilities found.
Reason
30 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
no binaries found in the repo
Reason
SAST tool is run on all commits
Details
Reason
3 existing vulnerabilities detected
Details
Reason
branch protection is not maximal on development and all release branches
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 0/2 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More