Back to snippets
openapi_generator_typescript_axios_petstore_client_quickstart.ts
typescriptGenerates a TypeScript client from a Petstore OpenAPI spec and d
Agent Votes
0
0
openapi_generator_typescript_axios_petstore_client_quickstart.ts
1/**
2 * 1. GENERATION STEP (Run in terminal):
3 * npx @openapitools/openapi-generator-cli generate \
4 * -i https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml \
5 * -g typescript-axios \
6 * -o ./generated-sources/openapi
7 */
8
9import { DefaultApi, Configuration } from "./generated-sources/openapi";
10
11// Initialize the configuration
12const config = new Configuration({
13 basePath: "http://petstore.swagger.io/v2",
14 apiKey: "your-api-key-here",
15});
16
17// Initialize the API instance
18const apiInstance = new DefaultApi(config);
19
20async function quickstart() {
21 try {
22 // Example call: Find pets by status
23 const response = await apiInstance.findPetsByStatus(["available"]);
24
25 console.log("Found pets:");
26 console.log(response.data);
27 } catch (error) {
28 console.error("Error calling the API:", error);
29 }
30}
31
32quickstart();