Clarifai API Clients
Clarifai API provides clients in the most popular languages.

Clarifai Clients

You can access the Clarifai API through clients in many of the most popular programming languages. Our clients are built on gRPC and are accessible through HTTP+JSON channels as well as gRPC channels.

Manually-built Clients (deprecated)

Available Clients
C#
Java
PHP

Client Installation Instructions

Here are installation instructions for three of our most commonly used clients. For information on installing our other clients, please follow the links above.

Authorization keys

The key to be used as authorization can be either:
  • An API key, which is tied to a certain application, or
  • A Personal Access Token (PAT), which is tied to a user.
Since a user can own multiple applications, using a PAT is more powerful. However, using a PAT also means that you need to specify the application ID to which the request should be applied.
With most endpoints you can freely choose whether to use an API key or a PAT. In this documentation, some code examples use one and some the other. But certain endpoints support only PAT (e.g. creating a new application or a new API key).
Python
PHP
Java
NodeJS
C#
1
##############################################################################
2
# Installation
3
##############################################################################
4
5
pip install clarifai-grpc
6
7
##############################################################################
8
## Initialize client
9
## - This initializes the gRPC based client to communicate with the
10
## Clarifai platform.
11
##############################################################################
12
## Import in the Clarifai gRPC based objects needed
13
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
14
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
15
from clarifai_grpc.grpc.api.status import status_pb2, status_code_pb2
16
17
## Construct the communications channel and the object stub to call requests on.
18
# Note: You can also use a secure (encrypted) ClarifaiChannel.get_grpc_channel() however
19
# it is currently not possible to use it with the latest gRPC version
20
channel = ClarifaiChannel.get_grpc_channel()
21
stub = service_pb2_grpc.V2Stub(channel)
22
23
24
################################################################################
25
## Set up Personal Access Token and Access information
26
## - This will be used by every Clarifai API call
27
################################################################################
28
## Specify the Authorization key. This should be changed to your Personal Access Token.
29
## Example: metadata = (('authorization', 'Key 123457612345678'),)
30
metadata = (('authorization', 'Key {YOUR_CLARIFAI_API_KEY}'),)
31
32
##
33
## A UserAppIDSet object is needed for most rpc calls. This object contains
34
## two pieces of information: the user id and the app id. Both of these are
35
## specified as string values.
36
##
37
## 'user_id' : This is your user id
38
## 'app_id' : This is the app id which contains the model of interest
39
userDataObject = resources_pb2.UserAppIDSet(user_id='{YOUR USER NAME HERE}', app_id='{YOUR APPLICATION ID HERE}')
Copied!
1
<?php
2
//////////////////////////////////////////////////////////////////////////////
3
// Installation
4
// - gRPC for PHP is required to use the Clarifai API
5
//////////////////////////////////////////////////////////////////////////////
6
7
8
9
10
//////////////////////////////////////////////////////////////////////////////
11
// Set Clarifai Namespace
12
// - A variety of standard objects are provided in the Clarifai namespace
13
// from the client library. Which ones that are necessary depend on the
14
// specific RPC call being made. All namespaces used in the example
15
// code are included below for reference, although you likely won't need
16
// all of these in your application.
17
//////////////////////////////////////////////////////////////////////////////
18
19
// Various data structures that are used in the RPC calls to the Clarifai Platform
20
// These operate as standardization wrappers for various types of data.
21
22
// Data Types
23
use Clarifai\Api\Image;
24
use Clarifai\Api\Text;
25
use Clarifai\Api\Video;
26
27
// ML Structures
28
use Clarifai\Api\Concept;
29
use Clarifai\Api\Model;
30
31
// Wrapper Types
32
use Clarifai\Api\Data;
33
use Clarifai\Api\Input;
34
35
// Various Request objects. These specify the structure of the actual RPC request between
36
// the client and the platform.
37
use Clarifai\Api\PostModelOutputsRequest;
38
use Clarifai\Api\PostConceptsSearchesRequest;
39
40
41
use Clarifai\Api\ConceptQuery;
42
43
// Output configuration objects
44
use Clarifai\Api\OutputInfo;
45
use Clarifai\Api\OutputConfig;
46
47
// The request status code object. This contains information on the success or failure of
48
// the API operation.
49
use Clarifai\Api\Status\StatusCode;
50
51
52
53
//////////////////////////////////////////////////////////////////////////////
54
// Initialize client
55
// - This initializes the gRPC based client to communicate with the
56
// Clarifai platform.
57
//////////////////////////////////////////////////////////////////////////////
58
59
// The Clarifai PHP Client repository includes an autoload.php helper file that needs to be included
60
require 'vendor/autoload.php';
61
62
// Enable use of the ClarifaiClient object from the Clarifai namespace
63
use Clarifai\ClarifaiClient;
64
65
// Construct the actual gRPC client object
66
$client = ClarifaiClient::grpc();
67
68
69
70
//////////////////////////////////////////////////////////////////////////////
71
// Set up Personal Access Token and Access information
72
// - This will be used by every Clarifai API call
73
//////////////////////////////////////////////////////////////////////////////
74
75
// Specify the Authorization key. This should be changed to your Personal Access Token.
76
// Example: $metadata = ['Authorization' => ['Key 123456789123456789']];
77
$metadata = ['Authorization' => ['Key {YOUR PERSONAL ACCESS TOKEN HERE}']]; // Using the PAT in these examples
78
79
//
80
// A UserAppIDSet object is needed for most rpc calls. This object cotnains
81
// two pieces of information: the user id and the app id. Both of these are
82
// specified as string values.
83
//
84
85
use Clarifai\Api\UserAppIDSet; // Specify the namespace for the UserAppIDSet object
86
87
$userDataObject = new UserAppIDSet([
88
'user_id' => '{YOUR USER NAME HERE}', // This is your user id
89
'app_id' => '{YOUR APPLICATION ID HERE}' // This is the app id which contains the model of interest
90
]);
Copied!
1
///////////////////////////////////////////////////////////////////////////////
2
// Installation (build.gradle)
3
///////////////////////////////////////////////////////////////////////////////
4
5
repositories {
6
jcenter()
7
}
8
9
dependencies {
10
implementation 'com.clarifai:clarifai-grpc:LATEST_VERSION'
11
}
12
13
///////////////////////////////////////////////////////////////////////////////
14
// Initialize client
15
///////////////////////////////////////////////////////////////////////////////
16
17
import com.clarifai.channel.ClarifaiChannel;
18
import com.clarifai.credentials.ClarifaiCallCredentials;
19
import com.clarifai.grpc.api.*;
20
import io.grpc.Channel;
21
22
23
Channel channel = ClarifaiChannel.INSTANCE.getGrpcChannel();
24
25
// Note: You can also use a secure (encrypted) ClarifaiChannel.INSTANCE.getGrpcChannel() however
26
// it is currently not supported in the latest gRPC version.
27
28
V2Grpc.V2BlockingStub stub = V2Grpc.newBlockingStub(channel)
29
.withCallCredentials(new ClarifaiCallCredentials("{YOUR_CLARIFAI_API_KEY}"));
Copied!
1
///////////////////////////////////////////////////////////////////////////////
2
// Installation
3
///////////////////////////////////////////////////////////////////////////////
4
5
npm install clarifai-nodejs-grpc
6
7
///////////////////////////////////////////////////////////////////////////////
8
// Initialize client
9
///////////////////////////////////////////////////////////////////////////////
10
11
const {ClarifaiStub, grpc} = require("clarifai-nodejs-grpc");
12
13
const stub = ClarifaiStub.grpc();
14
15
// This will be used by every Clarifai endpoint call.
16
const metadata = new grpc.Metadata();
17
metadata.set("authorization", "Key {YOUR_CLARIFAI_API_KEY}");
Copied!
1
///////////////////////////////////////////////////////////////////////////////
2
// Installation
3
///////////////////////////////////////////////////////////////////////////////
4
dotnet add package ClarifaiGrpc
5
6
///////////////////////////////////////////////////////////////////////////////
7
// Initialize client
8
///////////////////////////////////////////////////////////////////////////////
9
using System;
10
using System.Collections.Generic;
11
using Clarifai.Api;
12
using Clarifai.Channels;
13
using Grpc.Core;
14
using StatusCode = Clarifai.Api.Status.StatusCode;
15
16
var client = new V2.V2Client(ClarifaiChannel.Grpc());
17
18
var metadata = new Metadata
19
{
20
{"Authorization", "Key {YOUR_PERSONAL_TOKEN}"}
21
};
Copied!
Last modified 30d ago