Skip to main content

Node.js SDK

Discover the power of the Node.js SDK


The Clarifai Node.js SDK is your gateway for effortlessly integrating AI capabilities into your JavaScript and Node.js applications. Built using TypeScript, this SDK empowers you to easily interact with the Clarifai API using an intuitive and object-oriented approach.

With TypeScript's strong typing and modern features, you can automate your AI workflows with minimal code. It’s the ideal tool to bring AI-powered features to your Node.js projects.

Installation

You can install the latest stable version of the Clarifai Node.js SDK by running:

npm install clarifai-nodejs

Authentication

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

You can export your PAT as an environment variable. Then, import and initialize the API client.

Here is how to set your PAT as an environment variable through the terminal:

export CLARIFAI_PAT=YOUR_PAT_HERE

Alternatively, use dotenv to load environment variables from a .env file. This is useful for managing secrets in Node.js applications.

How to Use Node.js SDK in Next.js Server Components

To integrate the Clarifai Node.js SDK in a Next.js App Directory project with server components, you need to explicitly add the clarifai-nodejs-grpc package to the serverComponentsExternalPackages configuration in your next.config.js file.

This ensures that Next.js properly handles the gRPC dependencies required by the Clarifai SDK, allowing you to use AI-powered inference in your server-side components.

/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
serverComponentsExternalPackages: ['clarifai-nodejs-grpc'],
},
}

module.exports = nextConfig