clarifai-node-serverless
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

image

Clarifai Node.js SDK - For Serverless Environments

npm Build Discord

Clarifai Node.js SDK - For Serverless Environments

This is the official Node.js client library for working with Clarifai's API in serverless environments. This library is designed to be lightweight, compatible with modern ESM syntax, and optimized for serverless platforms like AWS Lambda, Vercel, and Netlify.

This library currently includes only a small subset of features from the main Clarifai Node.js SDK package. We are actively working on adding more features and improvements. If you need a specific feature, please let us know by opening an issue or joining our Discord community.

Website | Schedule Demo | Signup for a Free Account | API Docs | Clarifai Community | Discord

Give the repo a star ⭐

Installation

npm install clarifai-node-serverless

Usage

Clarifai uses Personal Access Tokens(PATs) to validate requests. You can create and manage PATs under your Clarifai account security settings.

  • 🔗 Create PAT: Log into Portal → Profile Icon → Security Settings → Create Personal Access Token → Set the scopes → Confirm

Export your PAT as an environment variable. Then, import and initialize the API Client.

Set PAT as environment variable through terminal:

export CLARIFAI_PAT={your personal access token}

or use dotenv to load environment variables via a .env file

Using Workflows

import { Workflow, getInputFromUrl } from "clarifai-node-serverless";

const GENERAL_WORKFLOW =
  "https://clarifai.com/my-org/celebrity-check/workflows/General";

const CELEBRITY_IMAGE = "https://samples.clarifai.com/celebrity.jpeg";

const workflow = new Workflow({
  url: GENERAL_WORKFLOW,
  authConfig: {
    pat: process.env.CLARIFAI_PAT!,
  },
});

const input = getInputFromUrl({
  url: CELEBRITY_IMAGE,
  inputType: "image",
});

const response = await workflow.predict({
  inputs: [input],
});

console.log(response);

Using Clarifai GRPC directly

If you were previously using the clarifai-nodejs-grpc package or you want to access the full range of Clarifai API features, you can use the GRPC client directly.

import {
  PostModelOutputsRequest,
  V2Client,
} from "clarifai-node-serverless/grpc/proto/clarifai/api/service";
import { ChannelCredentials, Metadata } from "@grpc/grpc-js";
import { StatusCode } from "clarifai-node-serverless/grpc/proto/clarifai/api/status/status_code";

const client = new V2Client(
  "api.clarifai.com:443",
  ChannelCredentials.createSsl(),
);

const postModelOutputsRequest = PostModelOutputsRequest.fromPartial({
  modelId: "aaa03c23b3724a16a56b629203edc62c",
  inputs: [
    {
      data: {
        image: {
          url: "https://samples.clarifai.com/dog2.jpeg",
        },
      },
    },
  ],
});

const metadata = new Metadata();
metadata.set("authorization", `key ${process.env.CLARIFAI_PAT}`);

client.postModelOutputs(postModelOutputsRequest, metadata, (err, response) => {
  if (err) {
    console.log("Error: " + err);
    return;
  }

  if (response?.status?.code !== StatusCode.SUCCESS) {
    console.log(
      "Received failed status: " +
        response?.status?.description +
        "\n" +
        response?.status?.details,
    );
    return;
  }

  console.log("results:");
  console.log(response?.outputs);
});

Package Sidebar

Install

npm i clarifai-node-serverless

Weekly Downloads

4

Version

0.1.0

License

none

Unpacked Size

31.1 MB

Total Files

106

Last publish

Collaborators

  • clarifai-eng