Typescript API Typings

Typescript Types that facilitate API interaction

ReservoirKit comes with built in api types to facilitate interacting with Reservoir APIs. The API types are useful for parsing api responses and determining what parameters an api accepts. Note that the types are exposed as part of ReservoirKit's Client package, leaving where and how you use it up to you. There's no requirement for React, or anything else, except for typescript. Below we'll dig into some examples:

Typing out parameters:

import { paths } from '@reservoir0x/reservoir-kit-client'

const parameters: paths['/tokens/v5']['get']['parameters']['query'] = {
  ...
}

With the Typescript types imported you can easily discover what query parameters an api might allow.

Parsing api responses:

import { paths } from '@reservoir0x/reservoir-kit-client'

const res = await fetch('https://api.reservoir.tools/tokens/v5?limit=20&collection=0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d')
const data = (await res.json())

const response = data as paths['/tokens/v5']['get']['responses']['200']['schema']

When it comes to converting the data returned by an api request into a typed response you can use the Typescript api types. Here we fetch from the tokens api endpoint and then cast the data into a tokens v5 response schema. The result is an easy to use, typed, json schema.


Did this page help you?