Skip to main content

Build a Sample App with APIMatic generated SDK

The complete code for our sample app can be found on GitHub.

Tips for building a sample app

Configure local dependency

Typically we run a command like npm install [x] to add dependicies to a project. Since we haven't published our SDK to a package manager (npm), we need to point to our local copy.

Determine the relative path to your local generated SDK and add it to package.json.

"petstorelib":  "file:../Petstore"

Your package.json will look something like this.

{
"name": "apimatic-sample-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"petstorelib": "file:../Petstore"
},
"keywords": [],
"author": "",
"license": "ISC"
}

Install dependencies

With your petstore dependency sorted, you can install it with the command.

npm install

Get list of pets

Go to the Petstore developer portal you generated and click on List Pets

Click Configure, then check Show Complete File and enter 1234 for the access token.

Configure code

Close the configure screen. The code sample can be copy/pasted into your app and is fully runnable.

I've added a console.log() so there is something to see in our terminal.

The final code looks like this.


import { ApiError, Client, PetsController } from 'petstorelib';

const client = new Client({
timeout: 0,
accessToken: '1234',
});

const petsController = new PetsController(client);
const limit = 10;

try {
const { result, ...httpResponse } = await petsController.listPets(limit);
// Get more response info...
// const { statusCode, headers } = httpResponse;
if(result?.length) {
console.log('Hello ' + result[0].name);
}
} catch(error) {
if (error instanceof ApiError) {
const errors = error.result;
// const { statusCode, headers } = error;
}
}

Next, compile your Typescript to JavaScript.

tsc --build

Run the code.

node index.js

This should display "Hello X" in the terminal. (X is randomly generated by our mock API server.)

Let's look at customizing generated documentation with APIMatic.

Share this page: