Connecting to VTEX Core Commerce APIs
Context
A VTEX IO app is a first-class citizen to connect with VTEX Core Commerce API's (https://developers.vtex.com/docs/api-reference), and it's very easy to create integrations with our modules.
The Client you need may already be implemented on
@vtex/clients
. Check it out on https://github.com/vtex/commerce-io-clients
Steps
- On your app, create a Client that represents the module you want to access. It will be a class that extends JanusClient.
- On the constructor, setup the
VtexIdclientAutCookie
header with the token you want to use to authorize the request. Usectx.authToken
to use the app's token. You may also usectx.vtex.storeUserAuthToken
orctx.vtex.adminUserAuthToken
to use the requester's token, for request incoming from the VTEX Admin or VTEX Storefront. You can also pass that on each specific call. - On each method, make the HTTP call using
this.http
using the path of the service you want to access. (e.g:/api/billing/company
) - On the app's
manifest.json
, add the appropriateoutbound-access
policy to the URL you are requesting. (example here) - That's it. Now, finish the Client's setup according to the documentation, and you may now use it on our code.
Keep In Mind 👀
GraphQL Apps
- Some of our Core Commerce Modules already have a GraphQL app that abstracts their endpoints, so it's important to check if the data you want to access is already exported via one of our GraphQL apps. You can check that on the GraphQL IDE app on your admin's (
vtex install vtex.admin-graphql-ide
if you can't see it)
Authentication
- IO Apps don't need appKey/appToken to make requests to VTEX APIs, thus it's not necessary to ask the merchant to create this pair. Every app will have it's own rotating token that can be used on the app's code.
- In some cases, it's not ideal to use the app's token (e.g: the authorization is important and depends on the calling user), and you can use the user's token instead, using
ctx.vtex.storeUserAuthToken
orctx.vtex.adminUserAuthToken
.