This guide will walk you through creating a minimal NodeJS project to integrate with Inferable. This is an example that steps through the core concepts of Inferable.

Prerequisites

Make sure you have Node.js installed and have set up your Inferable machine key as an environment variable (INFERABLE_API_SECRET).

You can get your API secret by running:

# npm install -g @inferable/cli
inf auth keys create --type=cluster_machine minimal_example_key
1

Install dependencies

Install the necessary packages:

npm init -y
npm install inferable zod
2

Initialize the Inferable client

Create a new file called index.ts and add the following code to initialize the Inferable client:

import { Inferable } from "inferable";
import { z } from "zod";

// Initialize the Inferable client
const client = new Inferable({
  apiSecret: process.env.INFERABLE_API_SECRET,
});
3

Register a function

Now, let’s add a function that greets users:

// Define the greeting function
client.default.register({
  name: "greetUser",
  description: "Greets a user by name",
  schema: {
    input: z.object({
    name: z.string(),
  }),
  },
  func: async (input: { name: string }) => {
    return `Hello, ${input.name}! Welcome to Inferable.`;
  },
});
4

Start the service

Finally, let’s start the service:

// Start the service
inferable.default.start().then(() => {
  console.log("Greeting service is running...");
})
5

Run the project

To run this example, save the file as app.js and execute it using Node.js:

INFERABLE_API_SECRET=****** tsx index.ts

Interacting with the service

Now that you have the service running, you can interact with it via: