High-Level API

Sia provides a high-level API that simplifies the serialization and deserialization process. Sia also provides a high-level API for Unchained Plugins, which allows making RPC calls over the network. While the RPC functionality is Unchained-specific, the rest of the high-level API can be used independently.

Basics

The high-level API makes use of a schema to define the structure of the data to be serialized. The schema is defined using a custom language, which is compiled to an intermediate representation that is used to generate the serialization and deserialization code.

To install the Sia compiler, run the following command:

This code installs the command globally, allowing you to compile Sia schemas. The compiler supports generating TypeScript code, with support for other languages planned for the future.

TypeScript

Consider the following Sia schema definition:

To compile the schema, run the following command:

Then, use the following code to serialize and deserialize a JavaScript object:

RPC

The high-level API also provides a way to define RPC methods using the Sia schema language. The following example defines an RPC method that takes a person's name and age and returns a greeting message:

Compiling the above code generates bindings for calling the RPC method. The following code demonstrates how to call the RPC method:

Instead of inlining the parameters and return types, you can define them in separate schemas and reference them in the method definition:

You can then generate the bindings using the Sia compiler and use them in your application:

How was this page?

Timeleap SA.

Pl. de l'Industrie 2, 1180 Rolle, Switzerland

Logo

Social Media

Tokenomics

Info