Documentation
Feedback
Guides
App Development

App Development
Node apps
Calling commerce APIs on VTEX IO3. Using VTEX IO clients
3. Using VTEX IO clients

Now equipped with knowledge on authenticating API calls in VTEX IO, let's dive into the practical aspect of calling VTEX Core Commerce APIs using native Clients. In this guide, we'll specifically leverage the Catalog client to retrieve SKU details within the VTEX platform.

In VTEX IO, Clients act as abstractions for external services, simplifying external requests in backend services. We strogly recommend using pre-built Clients from the @vtex/clients package for VTEX Core Commerce services and developing custom Clients for external providers (e.g., Google Maps API).

Installing the @vtex/clients package

The @vtex/clients package is a TypeScript library featuring pre-configured Clients for accessing VTEX Core Commerce APIs' capabilities. To use it in your app, change to the node folder and install the @vtex/clients package by running the command presented in the right panel.

Terminal

_10
cd node && yarn add @vtex/clients

Configuring the Catalog client

Update your app's client node/clients/index.ts by importing the Catalog Client from the @vtex/clients library and exporting it to your Clients object. From now on, any function in your app can use the Catalog Client via ctx.clients.catalog. Also, thanks to TypeScript typing capabilities, autocomplete will become available for the methods exported by the Catalog client.

Terminal
node/clients/index.ts

_15
import { IOClients } from '@vtex/api'
_15
import { Catalog } from '@vtex/clients'
_15
_15
import Status from './status'
_15
_15
// Extend the default IOClients implementation with our own custom clients.
_15
export class Clients extends IOClients {
_15
public get status() {
_15
return this.getOrSet('status', Status)
_15
}
_15
_15
public get catalog() {
_15
return this.getOrSet('catalog', Catalog)
_15
}
_15
}

{"base64":"  ","img":{"width":512,"height":259,"type":"jpg","mime":"image/jpeg","wUnits":"px","hUnits":"px","length":35149,"url":"https://cdn.jsdelivr.net/gh/vtexdocs/dev-portal-content@main/images/calling-commerce-apis-3-using-vtex-io-clients.jpeg"}}

Using the Catalog client

Now, let's use the getSkuById method from the Catalog client to retrieve information from a Stock Keeping Unit (SKU) in the VTEX Catalog. To do so, we will update the status route logic to read the code parameter as an SKU ID and return SKU details.

Open the node/middlewares/status.ts file and update it as in the following:

  • Extract the catalog client from the ctx received in the middleware functions. Learn more about Destructuring on the Mozilla docuentation.
  • Extract the variable code, which will be used as the code parameter of our route (/_v/status/:code).
  • Call the getSkuById method from the Catalog client. This method will internally call the necessary endpoints of the Catalog API, passing the code parameter as the SKU ID to be searched. Also, since this is an asynchronous call, ensure to use the await to wait for it.
Terminal
node/clients/index.ts
node/middlewares/status.ts

_11
export async function status(ctx: Context, next: () => Promise<any>) {
_11
const {
_11
state: { code },
_11
clients: { catalog },
_11
} = ctx
_11
_11
const data = await catalog.getSkuById(code.toString())
_11
ctx.body = data
_11
_11
await next()
_11
}

Declaring the necessary policies

Usually, native Clients are already automatically configured to make authenticated calls using the app token. Nevertheless, you still need to declare that your app will be making requests for some service.

To do so, open the manifest.json file and add the highlighted code in the right panel to the policies section. This will allow your app to make calls to the Catalog API.

Terminal
node/clients/index.ts
node/middlewares/status.ts
manifest.json

_34
{
_34
"name": "service-example",
_34
"vendor": "vtex",
_34
"version": "0.1.1",
_34
"title": "Service Example",
_34
"description": "Reference app for VTEX IO Services",
_34
"mustUpdateAt": "2018-01-04",
_34
"categories": [],
_34
"dependencies": {},
_34
"builders": {
_34
"node": "7.x",
_34
"docs": "0.x"
_34
},
_34
"scripts": {
_34
"prereleasy": "bash lint.sh"
_34
},
_34
"credentialType": "absolute",
_34
"policies": [
_34
{
_34
"name": "outbound-access",
_34
"attrs": {
_34
"host": "portal.vtexcommercestable.com.br",
_34
"path": "/api/catalog/*"
_34
}
_34
},
_34
{
_34
"name": "colossus-fire-event"
_34
},
_34
{
_34
"name": "colossus-write-logs"
_34
}
_34
],
_34
"$schema": "https://raw.githubusercontent.com/vtex/node-vtex-api/master/gen/manifest.schema"
_34
}

Fetching the SKU data

With the app linked to your development workspace, copy and send a GET request to the public URL that your service is exposing. Paste in the address bar https://{workspace}--{account}.myvtex.com/_v/status/1 to find information about the SKU ID 1. The expected result should be a JSON with the SKU data, fetched from the Catalog API.

Terminal
node/clients/index.ts
node/middlewares/status.ts
manifest.json

_10
16:39:05.042 - info: Worker 26 is listening service-node@6.27.4 16:39:05.567 info: Runtime @vtex/api is: /usr/local/app/node_modules/@vtex/api/lib/index.js service-node@6.27.4
_10
16:39:85.568 - info: Using @vtex/api from: /usr/local/data/service/node_modules/@vtex/api/lib/index.js service-node@6.27.4
_10
16:39:05.929 - info: Available service routes:
_10
https://myworkspace--myaccount.myvtex.com/_v/status/:code service-node@6.27.4
_10
16:39:05.932 - info: App running service-node@6.27.4
_10
16:39:85.956 - info: Debugger tunnel listening on :9229. Go to chrome://inspect in Google Chrome to debug your running application.

GET

_28
{
_28
"Id": 1,
_28
"ProductId": 1,
_28
"IsActive": true,
_28
"ActivateIfPossible": true,
_28
"Name": "Tanque Duplo Tramontina Hera Duo Plus 2C 30 + 30 L em Aço Inox Acetinado 120 x 55 cm",
_28
"RefId": "94406117",
_28
"PackagedHeight": 1,
_28
"PackagedLength": 1,
_28
"PackagedWidth": 1,
_28
"PackagedWeightKg": 20000,
_28
"Height": null,
_28
"Length": null,
_28
"Width": null,
_28
"WeightKg": null,
_28
"CubicWeight": 1,
_28
"IsKit": false,
_28
"CreationDate": "2023-07-12T09:15:00",
_28
"RewardValue": null,
_28
"EstimatedDateArrival": null,
_28
"ManufacturerCode": "94406117",
_28
"CommercialConditionId": 1,
_28
"MeasurementUnit": "un",
_28
"UnitMultiplier": 1,
_28
"ModalType": "FURNITURE",
_28
"KitItensSellApart": false,
_28
"Videos": []
_28
}

Installing the @vtex/clients package

The @vtex/clients package is a TypeScript library featuring pre-configured Clients for accessing VTEX Core Commerce APIs' capabilities. To use it in your app, change to the node folder and install the @vtex/clients package by running the command presented in the right panel.

Configuring the Catalog client

Update your app's client node/clients/index.ts by importing the Catalog Client from the @vtex/clients library and exporting it to your Clients object. From now on, any function in your app can use the Catalog Client via ctx.clients.catalog. Also, thanks to TypeScript typing capabilities, autocomplete will become available for the methods exported by the Catalog client.

{"base64":"  ","img":{"width":512,"height":259,"type":"jpg","mime":"image/jpeg","wUnits":"px","hUnits":"px","length":35149,"url":"https://cdn.jsdelivr.net/gh/vtexdocs/dev-portal-content@main/images/calling-commerce-apis-3-using-vtex-io-clients.jpeg"}}

Using the Catalog client

Now, let's use the getSkuById method from the Catalog client to retrieve information from a Stock Keeping Unit (SKU) in the VTEX Catalog. To do so, we will update the status route logic to read the code parameter as an SKU ID and return SKU details.

Open the node/middlewares/status.ts file and update it as in the following:

  • Extract the catalog client from the ctx received in the middleware functions. Learn more about Destructuring on the Mozilla docuentation.
  • Extract the variable code, which will be used as the code parameter of our route (/_v/status/:code).
  • Call the getSkuById method from the Catalog client. This method will internally call the necessary endpoints of the Catalog API, passing the code parameter as the SKU ID to be searched. Also, since this is an asynchronous call, ensure to use the await to wait for it.

Declaring the necessary policies

Usually, native Clients are already automatically configured to make authenticated calls using the app token. Nevertheless, you still need to declare that your app will be making requests for some service.

To do so, open the manifest.json file and add the highlighted code in the right panel to the policies section. This will allow your app to make calls to the Catalog API.

Fetching the SKU data

With the app linked to your development workspace, copy and send a GET request to the public URL that your service is exposing. Paste in the address bar https://{workspace}--{account}.myvtex.com/_v/status/1 to find information about the SKU ID 1. The expected result should be a JSON with the SKU data, fetched from the Catalog API.

Terminal

_10
cd node && yarn add @vtex/clients

Contributors
2
Photo of the contributor
Photo of the contributor
+ 2 contributors
Was this helpful?
Yes
No
Suggest Edits (GitHub)
Contributors
2
Photo of the contributor
Photo of the contributor
+ 2 contributors
On this page