Typescript
You can use the openapi-typescript package to generate a TypeScript client from the OpenAPI specification of the Rest API. This will allow you to have type-safe access to the API endpoints and data structures.
/package.json
{
"dependencies": {
"openapi-fetch": "^0.15.0",
"openapi-typescript": "^7.10.0"
}
}
Run the following command to generate the TypeScript types from the OpenAPI specification:
npm run openapi-typescript https://api.kimzee.com/docs/swagger.json -o ./schema.d.ts
Here is an example of how to use the generated client to make a request to the Rest API:
/src/client.ts
import createClient from "openapi-fetch";
import { paths } from "./schema";
const client = createClient<paths>({ baseUrl: "https://api.kimzee.com/api/v1/" });
const authorize = {
onRequest: ({ request }) => {
request.headers.set("X-API-Key", process.env.API_KEY);
return request;
},
};
client.use(authorize);
// Will fetch all the campaign accessible with the provided API key
const { data, error } = await client.GET("/campaign");
if (error) {
console.error(error);
process.exit(1);
}
const exampleCampaign = data.find(({ name }) => name === "Campaign Test");
// Will submit a new entry to the campaign with the provided resourceId and body
await client.POST("/campaign/{resourceId}/submit", {
params: {
path: {
resourceId: exampleCampaign.resource.id,
},
},
body: {
data: {
email: "test@example.com",
name: "Test User",
birthday: "1997-10-17",
company: "My Company",
technology: "TypeScript",
},
},
});