ICSSC LogoICS Student Council

TypeScript Integration

Our API comes fully equipped with an automatically generated OpenAPI specification. This means that with the right tools, you can ensure that your requests are being made with the correct parameters, and that your responses have what you expect - and all without hardcoding any types.

Getting Started

For this guide, we'll be using openapi-typescript, an open-source library that consumes OpenAPI specifications and outputs TypeScript type definitions. We'll need to install that first.

Run the following command in your project:

npm install -D openapi-typescript

Next, you'll want to generate the type definitions, like so:

npx openapi-typescript https://anteaterapi.com/openapi.json \
--output src/types/generated/anteater-api-types.ts

I've chosen to output the types into src/types/generated for convenience, but you can put the output anywhere that makes sense. Regardless of where you put it, you should add it to your project's .gitignore if you're using version control.

Now that you have the type definitions, you can create type aliases for the types you'll be using. Let's say you want to manipulate course data, so you'll need the types of the request object and of the response objects. This can be accomplished like so:

src/types/course.ts
import type { paths } from "./generated/anteater-api-types";
 
type  = paths["/v2/rest/courses"]["get"]["parameters"]["query"];
 
type  = paths["/v2/rest/courses"]["get"]["responses"][200]["content"]["application/json"];

As you can see by hovering over the CourseParams and CourseResponse type aliases, they have the shape that you would expect, and you can use them to ensure the type-safety when interacting with the API.

On this page