#web-assembly #wasm #wit #wasm-interface-types

bin+lib witme

Tools for generating to and from wit format

13 unstable releases (3 breaking)

0.3.1 May 18, 2022
0.3.0 May 13, 2022
0.2.6 May 12, 2022
0.2.5 Apr 21, 2022
0.0.2 Feb 14, 2022

#680 in WebAssembly

Download history 10/week @ 2022-12-02 15/week @ 2022-12-09 20/week @ 2022-12-16 9/week @ 2022-12-23 5/week @ 2022-12-30 7/week @ 2023-01-06 5/week @ 2023-01-13 13/week @ 2023-01-20 11/week @ 2023-01-27 19/week @ 2023-02-03 36/week @ 2023-02-10 40/week @ 2023-02-17 9/week @ 2023-02-24 10/week @ 2023-03-03 34/week @ 2023-03-10 6/week @ 2023-03-17

60 downloads per month
Used in raen

MIT license

2.5K SLoC

Rust 2K SLoC // 0.0% comments TypeScript 343 SLoC // 0.1% comments JavaScript 241 SLoC // 0.0% comments


Tools for generating to and from wit format.

Option 1

cargo install witme

If generating json schemas need nodejs installed. One suggestion is to use nvm.

Option 2

npm i -g witme


Currently this repo is geared toward NEAR smart contracts (currently only for Rust), but the goal is to become a general purpose tool for working with the .wit format.

  • Generate .wit from NEAR smart contracts written in Rust
  • Generate TS from .wit for interacting with contracts
  • Generate JSON Schema from TS (in the future will be directly from wit)


Currently there is a near subcommand for dealing with NEAR related transformations.

  • witme near wit
    • generates a index.wit file in the root of a rust project (note: it can't be a workspace). This builds on witgen.
  • witme near ts
    • generates ts files from a .wit file (defaults index.wit -> ./ts/*). This builds on wit-bindgen
  • witme near json
    • generates a json schema for the inputs to a schema from the typescript (defaults ./ts/index.ts --> index.schema.json), which uses ts-json-schema-generator.
  • witme near inject
    • writes data into a the custom section of a Wasm binary.


The generated json schema can be used to automatically generate a react form which can validate the arguments to a contract call. Since .wit has a notion of documentation comments the documentation provided by the Rust source will be available in the generated TS and Json schema. This also allows special annotations which can add more conditions on the types used in the contract.

For example,

///  @minLength 2
///  @maxLength 64
///  @pattern ^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$
type AccountId = String

Generates the following wit:

///  @minLength 2
///  @maxLength 64
///  @pattern ^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$
type account-id = string

which generates the following typescript:

* @minLength 2
* @maxLength 64
* @pattern ^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$
export declare type AccountId = string;

which generates the following json schema:

  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "AccountId": {
      "maxLength": 64,
      "minLength": 2,
      "pattern": "^(([a-z\\d]+[-_])*[a-z\\d]+\\.)*([a-z\\d]+[-_])*[a-z\\d]+$",
      "type": "string"

Consider the rust-status-message example in this repo:

/// Retreive a message for a given account id
pub fn get_status(&self, account_id: AccountId) -> Option<String> {

generates the following schema for its arguments:

  "GetStatus": {
      "additionalProperties": false,
      "contractMethod": "view",
      "description": "Retreive a message for a given account id",
      "properties": {
        "account_id": {
          "$ref": "#/definitions/AccountId"
      "required": [
      "type": "object"

And the following TS method on the generated Contract class:

    * Retreive a message for a given account id
    get_status(args: {
        account_id: AccountId;
    }, options?: ViewFunctionOptions): Promise<string | null>;

See close-up for an example for generating forms from a schema.


Currently .wit doesn't prescribe how variant types are implemented for a language. witme currently supports JSON encoded arguments and return values. Thus variants are currently encoded the same as the defaults provided by serde_json. However, in the future borsh support would remove this restriction and allow variant types to be encoded more efficiently.


  • Provide a generator for Rust to allow both cross contract calls and client/tests implementations.
  • Generate json schema directly from wit
  • Have validation be generated in Rust and compiled to Wasm to simplify the validation and allow transformations like encoding to borsh to be handled behind the scenes.


~207K SLoC