SDK JS

A complete training 101 is available on GitHub.com

What is it?

The GreyCat JS SDK is the official JavaScript (and TypeScript) library used by application to communicate with a GreyCat Server.

It comes with the generated bindings for the official libraries: std and algebra.

This library is packaged as an headless ESM module, meaning that is does not require to be run in a browser environment. It is compatible with Node.js (and Bun, deno) and all major Web browsers.

Getting started

touch package.json

Then paste the following content in package.json:

{
  "name": "greycat-app",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "dependencies": {
    "@greycat/sdk": "https://get.greycat.io/files/sdk/js/dev/6.5/6.5.14-dev.tgz"
  }
}

And install the dependency with:

npm i

First steps

Create a GreyCat project file named project.gcl with the following content:

@expose
fn hello(name: String): String {
  return "Hello, ${name}";
}

And start the server with this command:

greycat serve --user=1

--user=1 means that every requests are impersonated to be the root user, effectively bypassing authentication as this is out of the scope of this intro.

Create a JavaScript entry point file named index.js, paste the following:

// 'GreyCat' is main class exposed by the SDK
import { GreyCat } from '@greycat/sdk';

// It gives you a static method to be called in order to initiate
// the connection with a GreyCat server. Without any options, it
// will try to connect to http://127.0.0.1:8080, which is the default.
const greycat = await GreyCat.init();

// Call an exposed endpoint using the `call('module::name', [...args])` method
const hello = await greycat.call('project::hello', [' world!']);

// Say hello!
console.log(hello);

And run that file using Node.js:

node index.js
# Hello, world!

If you see Hello, world! in the console, you effectively used the JS SDK to communicate with a remote GreyCat server.