Clarifai JavaScript Client
The official JavaScript client for interacting with the Clarifai API.
Basic Use
To start, install the SDK via NPM: npm install clarifai
and initialize with your clientId and
clientSecret:
This will work in node.js and browsers via Browserify
var Clarifai = require('clarifai');
Clarifai.initialize({
'clientId': '{clientId}',
'clientSecret': '{clientSecret}'
});
You can also use the SDK by adding this script to your HTML:
<script type="text/javascript" src="https://sdk.clarifai.com/js/clarifai-1.2.0.js"></script>
<script>
Clarifai.initialize({
'clientId': '{clientId}',
'clientSecret': '{clientSecret}'
});
</script>
Table of Contents
Tag
Info
Languages
Color
Usage
Feedback
Token
Promises and Callbacks
Examples
Tag
Get tags for an image via url
Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg').then(
handleResponse,
handleError
);
Get tags for multiple images via url
Clarifai.getTagsByUrl([
'https://samples.clarifai.com/wedding.jpg',
'https://samples.clarifai.com/cookies.jpeg'
]).then(
handleResponse,
handleError
);
Get tags for an image via image bytes
Clarifai.getTagsByImageBytes('R0lGODlhZAHIAPcAAKeno6Oinc3Do6iVeMe7o1ZEM...').then(
handleResponse,
handleError
);
Get tags for an image via url passing in a model
Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg', {
'model': 'nsfw-v0.1'
}).then(
handleResponse,
handleError
);
Get tags for an image via url passing in a language
Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg', {
'language': 'es'
}).then(
handleResponse,
handleError
);
Get tags for an image via url and set a localId
Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg', {
'localId': 'myLocalId'
}).then(
handleResponse,
handleError
);
Get tags for an image via url and restrict the tags returned
Clarifai.getTagsByUrl(
'https://samples.clarifai.com/wedding.jpg',
{
'selectClasses': ['people', 'dress', 'wedding']
}
).then(
handleResponse,
handleError
);
Info
Get API info
Clarifai.getInfo().then(
handleResponse,
handleError
);
Languages
Get supported languages
Clarifai.getLanguages().then(
handleResponse,
handleError
);
Color
Get colors for an image via url
Clarifai.getColorsByUrl('https://samples.clarifai.com/wedding.jpg').then(
handleResponse,
handleError
);
Get colors for an image via image bytes
Clarifai.getColorsByImageBytes('R0lGODlhZAHIAPcAAKeno6Oinc3Do6iVeMe7o1ZEM...').then(
handleResponse,
handleError
);
Usage
Get API usage
Clarifai.getUsage().then(
handleResponse,
handleError
);
Feedback
Send feedback to the API
Clarifai.createFeedback('https://samples.clarifai.com/wedding.jpg', {
'addTags': ['family', 'friends',],
'removeTags': ['military', 'protest'],
}).then(
handleResponse,
handleError
);
Token
Get a token
Note: You should not have to call this directly in most cases. Any method that needs a token will call
it for you.
Clarifai.getToken().then(
function(response) {
console.log(response);
},
function(err){
console.log(err);
}
);
Set a token
var tokenSetBoolean = Clarifai.setToken('some-token-string');
Delete a token
Clarifai.deleteToken();
Promises and Callbacks
All methods return promises. If you'd rather user callbacks, just pass in a callback function as the last
param to any method. If there are multiple params and some are optional, you'll need to pass in null
for
those.