In case you're adding a Kiota client to an existing project, the following configuration is required:
tsconfig > compilerOptions > esModuleInterop set to "true".
tsconfig > compilerOptions > forceConsistentCasingInFileNames set to "true".
tsconfig > compilerOptions > lib with an entry of "es2015".
tsconfig > compilerOptions > module set to "NodeNext".
tsconfig > compilerOptions > moduleResolution set to "NodeNext".
tsconfig > compilerOptions > target set to "es2020" or later.
package.json > type set to "module".
Add dependencies
Before you can compile and run the generated API client, you need to make sure the generated source files are part of a project with the required dependencies. Your project must have a reference to the bundle package. For more information about Kiota dependencies, see the dependencies documentation.
Run the following commands to get the required dependencies.
npm install @microsoft/kiota-bundle
Generate the API client
Kiota generates API clients from OpenAPI documents. Create a file named posts-api.yml and add the following.
Add --exclude-backward-compatible
if you want to reduce the size of the generated client and are not concerned about
potentially source breaking changes with future versions of Kiota when updating the client.
Create the client application
Create a file in the root of the project named index.ts and add the following code.
import { AnonymousAuthenticationProvider } from "@microsoft/kiota-abstractions";
import { FetchRequestAdapter } from "@microsoft/kiota-http-fetchlibrary";
import { createPostsClient } from "./client/postsClient.js";
import { Post } from "./client/models/index.js";
// API requires no authentication, so use the anonymous
// authentication provider
const authProvider = new AnonymousAuthenticationProvider();
// Create request adapter using the fetch-based implementation
const adapter = new FetchRequestAdapter(authProvider);
// Create the API client
const client = createPostsClient(adapter);
async function main(): Promise<void> {
try {
// GET /posts
const allPosts = await client.posts.get();
console.log(`Retrieved ${allPosts?.length} posts.`);
// GET /posts/{id}
const specificPostId = 5;
const specificPost = await client.posts.byPostId(specificPostId).get();
console.log(
`Retrieved post - ID: ${specificPost?.id}, Title: ${specificPost?.title}, Body: ${specificPost?.body}`
);
// POST /posts
const newPost: Post = {
userId: 42,
title: "Testing Kiota-generated API client",
body: "Hello world!",
};
const createdPost = await client.posts.post(newPost);
console.log(`Created new post with ID: ${createdPost?.id}`);
// PATCH /posts/{id}
const update: Post = {
// Only update title
title: "Updated title",
};
const updatedPost = await client.posts
.byPostId(specificPostId)
.patch(update);
console.log(
`Updated post - ID: ${updatedPost?.id}, Title: ${updatedPost?.title}, Body: ${updatedPost?.body}`
);
// DELETE /posts/{id}
await client.posts.byPostId(specificPostId).delete();
} catch (err) {
console.log(err);
}
}
main();
Note
The JSONPlaceholder REST API doesn't require any authentication, so this sample uses the AnonymousAuthenticationProvider. For APIs that require authentication, use an applicable authentication provider.
Run the application
To start the application, run the following command in your project directory.
Learn how using TypeScript for JavaScript development can help you build more robust code, reduce runtime type errors, take advantage of modern features before they are available in JavaScript, and work better with development teams.