Skip to content

Generating Types from ABI

Installation

First we install fuels to our project:

console
pnpm add fuels@0.95.0

Help

A first glance at the docs:

console
$ pnpm fuels typegen -h

Usage: fuels typegen [options]

Generate Typescript from Sway ABI JSON files

Options:
  -i, --inputs <path|glob...>  Input paths/globals to your ABI JSON files
  -o, --output <dir>           Directory path for generated files
  -c, --contract               Generate types for Contracts [default]
  -s, --script                 Generate types for Scripts
  -p, --predicate              Generate types for Predicates
  -S, --silent                 Omit output messages
  -h, --help                   Display help

Generating Types for Contracts

You can generate types for a Sway contract using the command below:

console
pnpm fuels typegen -i ./abis/*-abi.json -o ./types

The path after the input flag -i should point to the file ending in -abi.json produced when the contract was built.

The path after the output flag -o will be the output directory for the generated types.

You can omit the --contract option here since it's the default.

Generating Types for Scripts

To generate types for a Sway script, use the --script flag:

console
pnpm fuels typegen -i ./abis/*-abi.json -o ./types --script

Generating Types for Predicates

To generate types for a Sway predicate, use the --predicate flag:

console
pnpm fuels typegen -i ./abis/*-abi.json -o ./types --predicate

See also: