Learn how to enable seamless HTTP communication with VTEX Core Commerce APIs within your VTEX IO app.
VTEX IO apps are designed designed to seamlessly interface with VTEX Core Commerce APIs. These APIs provide a set of functionalities and data access points for managing and interacting with the VTEX ecommerce platform, enabling developers to perform various operations, such as managing orders, products, and customer data.
In this guide, you will learn how to create a custom client that can handle HTTP requests to these VTEX Core Commerce APIs, allowing your application to retrieve and manipulate data within the VTEX ecosystem.
Before you begin
Ensure a smooth development process by having the following prerequisites in place:
- VTEX IO Development Workspace: Set up your VTEX IO environment by following the steps outlined in Creating a Development Workspace.
- TypeScript Familiarity: Acquire a basic understanding of TypeScript, as we'll be using the
node
Builder for TypeScript development. For details, refer to Typescript's Official Documentation. - Understanding of Clients: Clients play a crucial role in facilitating interactions between your application and both external and internal services. Learn more about Clients.
Instructions
Step 1 - Setting up your VTEX IO app
-
Start a new VTEX IO app using the
node
builder and open the project using your preferred code editor. -
Install the
@vtex/api
package by running the following command:_10yarn add @vtex/api -
Update the app's
manifest.json
to include the appropriateoutbound-access
policy for the requested URL. Here's a hypothetical example:
Step 2 - Creating a Client for connecting to VTEX Core Commerce APIs
-
Create a TypeScript file for your Client in the
node/clients
directory. Choose a name that easily identifies your Client (e.g.,myClient.ts
). -
Create a client that represents the module you want to access. It will be a class that extends
JanusClient
. -
In the constructor, set the
VtexIdclientAutCookie
header with the required token for authorization. Usectx.authToken
for the app's token, orctx.vtex.storeUserAuthToken
orctx.vtex.adminUserAuthToken
for requests from VTEX Admin or VTEX Storefront, respectively. Refer to App authentication using auth tokens for more information.Ensure to export the Client from its module using either default or named export.
Step 3 - Implementing the Client methods
In your Client TypeScript file, implement the desired methods using the HttpClient
for targeted HTTP calls.
In the provided example, the newOrderForm
method is implemented to make HTTP requests using this.http
. It facilitates the creation of a new order form by sending a POST request (postRaw
) to the specified endpoint. The method includes additional configurations such as defining the metric for tracking and handling potential errors in the response.
Step 4 - Exporting custom clients
Now that you've created your custom Client, organize and export it for use in your VTEX IO service. Follow these steps:
-
Create an
index.ts
file in thenode/clients
folder. -
Inside the
index.ts
file, import the custom client you created in the previous step. For example:_10import MyClient from "./myClient.ts"; -
Define a class called
Clients
that extendsIOClients
. This class is used to organize and configure your custom Clients. Within the Clients class, declare aget
property for each of your custom Clients. This property allows you to access a specific Client by name. In our example, we've created a client namedmyClient
that uses theMyClient
class._10import { IOClients } from "@vtex/api";_10import MyClient from "./myClient.ts";_10_10export class Clients extends IOClients {_10public get status() {_10return this.getOrSet("myClient", MyClient);_10}_10}
Now that you have developed and exported your custom Client to communicate with the desired service, your Clients can be accessed and used within your VTEX IO service to perform various tasks. Learn how to use clients effectively in the Using Node Clients guide.
Key considerations
- GraphQL apps: Some Core Commerce modules already feature a GraphQL app that abstracts their endpoints. Check if the desired data is available via one of our GraphQL apps. Utilize the GraphQL IDE app on the Admin for exploration.
- Authentication: IO apps do not require an appKey/appToken pair to make requests to VTEX Core Commerce APIs. Every app has its own rotating token that can be used on the app's code. In scenarios where using the app's token is not ideal (e.g., authorization depends on the calling user), opt to use the user's token instead, using
ctx.vtex.storeUserAuthToken
orctx.vtex.adminUserAuthToken
. Refer to Authentication for more information.