|
| 1 | +# Clarifai JavaScript Client |
| 2 | + |
| 3 | +The official JavaScript client for interacting with the [Clarifai API](https://developer.clarifai.com). |
| 4 | + |
| 5 | +## Basic Use |
| 6 | + |
| 7 | +To start, install the SDK via NPM: `npm install clarifai-official` and initialize with your clientId and |
| 8 | +clientSecret: |
| 9 | + |
| 10 | +*This will work in node.js and browsers via [Browserify](http://browserify.org/)* |
| 11 | + |
| 12 | +```js |
| 13 | +var Clarifai = require('clarifai-official'); |
| 14 | + |
| 15 | +Clarifai.initialize({ |
| 16 | + 'clientId': '{clientId}', |
| 17 | + 'clientSecret': '{clientSecret}' |
| 18 | +}); |
| 19 | + |
| 20 | +``` |
| 21 | + |
| 22 | +You can also use the SDK by adding this script to your HTML: |
| 23 | + |
| 24 | +```js |
| 25 | +<script type="text/javascript" src="https://sdk.clarifai.com/js/clarifai-0.0.14.js"></script> |
| 26 | +<script> |
| 27 | + Clarifai.initialize({ |
| 28 | + 'clientId': '{clientId}', |
| 29 | + 'clientSecret': '{clientSecret}' |
| 30 | + }); |
| 31 | +</script> |
| 32 | +``` |
| 33 | + |
| 34 | +## Table of Contents |
| 35 | + |
| 36 | +#### Tag |
| 37 | + |
| 38 | +* [Get tags for an image via URL](#get-tags-for-an-image-via-url) |
| 39 | +* [Get tags for an image via bytes](#get-tags-for-an-image-via-image-bytes) |
| 40 | +* [Get tags for multiple images via url](#get-tags-for-multiple-images-via-url) |
| 41 | +* [Get tags for an image via url passing in a model](#get-tags-for-an-image-via-url-passing-in-a-model) |
| 42 | +* [Get tags for an image via url passing in a language](#get-tags-for-an-image-via-url-passing-in-a-language) |
| 43 | +* [Get tags for an image via url and set a localId](#get-tags-for-an-image-via-url-and-set-a-localid) |
| 44 | +* [Get tags for an image via url and restrict the tags returned](#get-tags-for-an-image-via-url-and-restrict-the-tags-returned) |
| 45 | + |
| 46 | +#### Info |
| 47 | + |
| 48 | +* [Get API info](#get-api-info) |
| 49 | + |
| 50 | +#### Languages |
| 51 | + |
| 52 | +* [Get supported languages](#get-supported-languages) |
| 53 | + |
| 54 | +#### Color |
| 55 | + |
| 56 | +* [Get colors for an image via url](#get-colors-for-an-image-via-url) |
| 57 | + |
| 58 | +#### Usage |
| 59 | + |
| 60 | +* [Get API usage](#get-api-usage) |
| 61 | + |
| 62 | +#### Feedback |
| 63 | + |
| 64 | +* [Send feedback to the API](#send-feedback-to-the-api) |
| 65 | + |
| 66 | +#### Token |
| 67 | + |
| 68 | +* [Get a token](#get-a-token) |
| 69 | +* [Set a token](#set-a-token) |
| 70 | +* [Delete a token](#delete-a-token) |
| 71 | + |
| 72 | +#### Promises and Callbacks |
| 73 | + |
| 74 | +* [Instructions](#promises-and-callbacks) |
| 75 | + |
| 76 | +## Examples |
| 77 | + |
| 78 | +### Tag |
| 79 | + |
| 80 | +#### Get tags for an image via url |
| 81 | + |
| 82 | +```js |
| 83 | +Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg').then( |
| 84 | + handleResponse, |
| 85 | + handleError |
| 86 | +); |
| 87 | +``` |
| 88 | + |
| 89 | +#### Get tags for multiple images via url |
| 90 | + |
| 91 | +```js |
| 92 | +Clarifai.getTagsByUrl([ |
| 93 | + 'https://samples.clarifai.com/wedding.jpg', |
| 94 | + 'https://samples.clarifai.com/cookies.jpeg' |
| 95 | +]).then( |
| 96 | + handleResponse, |
| 97 | + handleError |
| 98 | +); |
| 99 | +``` |
| 100 | + |
| 101 | +#### Get tags for an image via image bytes |
| 102 | + |
| 103 | +```js |
| 104 | +Clarifai.getTagsByImageBytes('R0lGODlhZAHIAPcAAKeno6Oinc3Do6iVeMe7o1ZEM...').then( |
| 105 | + handleResponse, |
| 106 | + handleError |
| 107 | +); |
| 108 | +``` |
| 109 | + |
| 110 | +#### Get tags for an image via url passing in a model |
| 111 | + |
| 112 | +```js |
| 113 | +Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg', { |
| 114 | + 'model': 'nsfw-v0.1' |
| 115 | +}).then( |
| 116 | + handleResponse, |
| 117 | + handleError |
| 118 | +); |
| 119 | +``` |
| 120 | + |
| 121 | +#### Get tags for an image via url passing in a language |
| 122 | + |
| 123 | +```js |
| 124 | +Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg', { |
| 125 | + 'language': 'es' |
| 126 | +}).then( |
| 127 | + handleResponse, |
| 128 | + handleError |
| 129 | +); |
| 130 | +``` |
| 131 | + |
| 132 | +#### Get tags for an image via url and set a localId |
| 133 | + |
| 134 | +```js |
| 135 | +Clarifai.getTagsByUrl('https://samples.clarifai.com/wedding.jpg', { |
| 136 | + 'localId': 'myLocalId' |
| 137 | +}).then( |
| 138 | + handleResponse, |
| 139 | + handleError |
| 140 | +); |
| 141 | +``` |
| 142 | + |
| 143 | +#### Get tags for an image via url and restrict the tags returned |
| 144 | + |
| 145 | +```js |
| 146 | +Clarifai.getTagsByUrl( |
| 147 | + 'https://samples.clarifai.com/wedding.jpg', |
| 148 | + { |
| 149 | + 'selectClasses': ['people', 'dress', 'wedding'] |
| 150 | + } |
| 151 | +).then( |
| 152 | + handleResponse, |
| 153 | + handleError |
| 154 | +); |
| 155 | +``` |
| 156 | + |
| 157 | +### Info |
| 158 | + |
| 159 | +#### Get API info |
| 160 | + |
| 161 | +```js |
| 162 | +Clarifai.getInfo().then( |
| 163 | + handleResponse, |
| 164 | + handleError |
| 165 | +); |
| 166 | +``` |
| 167 | + |
| 168 | +### Languages |
| 169 | + |
| 170 | +#### Get supported languages |
| 171 | + |
| 172 | +```js |
| 173 | +Clarifai.getLanguages().then( |
| 174 | + handleResponse, |
| 175 | + handleError |
| 176 | +); |
| 177 | +``` |
| 178 | + |
| 179 | +### Color |
| 180 | + |
| 181 | +#### Get colors for an image via url |
| 182 | + |
| 183 | +```js |
| 184 | +Clarifai.getColorsByUrl('https://samples.clarifai.com/wedding.jpg').then( |
| 185 | + handleResponse, |
| 186 | + handleError |
| 187 | +); |
| 188 | +``` |
| 189 | + |
| 190 | +#### Get colors for an image via image bytes |
| 191 | + |
| 192 | +```js |
| 193 | +Clarifai.getColorsByImageBytes('R0lGODlhZAHIAPcAAKeno6Oinc3Do6iVeMe7o1ZEM...').then( |
| 194 | + handleResponse, |
| 195 | + handleError |
| 196 | +); |
| 197 | +``` |
| 198 | + |
| 199 | +### Usage |
| 200 | + |
| 201 | +#### Get API usage |
| 202 | + |
| 203 | +```js |
| 204 | +Clarifai.getUsage().then( |
| 205 | + handleResponse, |
| 206 | + handleError |
| 207 | +); |
| 208 | +``` |
| 209 | + |
| 210 | +### Feedback |
| 211 | + |
| 212 | +#### Send feedback to the API |
| 213 | + |
| 214 | +```js |
| 215 | +Clarifai.createFeedback('https://samples.clarifai.com/wedding.jpg', { |
| 216 | + 'addTags': ['family', 'friends',], |
| 217 | + 'removeTags': ['military', 'protest'], |
| 218 | +}).then( |
| 219 | + handleResponse, |
| 220 | + handleError |
| 221 | +); |
| 222 | +``` |
| 223 | + |
| 224 | +### Token |
| 225 | + |
| 226 | +#### Get a token |
| 227 | + |
| 228 | +**Note:** You should not have to call this directly in most cases. Any method that needs a token will call |
| 229 | +it for you. |
| 230 | + |
| 231 | +```js |
| 232 | +Clarifai.getToken().then( |
| 233 | + function(response) { |
| 234 | + console.log(response); |
| 235 | + }, |
| 236 | + function(err){ |
| 237 | + console.log(err); |
| 238 | + } |
| 239 | +); |
| 240 | +``` |
| 241 | + |
| 242 | +#### Set a token |
| 243 | + |
| 244 | +```js |
| 245 | +var tokenSetBoolean = Clarifai.setToken('some-token-string'); |
| 246 | +``` |
| 247 | + |
| 248 | +#### Delete a token |
| 249 | + |
| 250 | +```js |
| 251 | +Clarifai.deleteToken(); |
| 252 | +``` |
| 253 | + |
| 254 | +### Promises and Callbacks |
| 255 | + |
| 256 | +All methods return promises. If you'd rather user callbacks, just pass in a callback function as the last |
| 257 | +param to any method. If there are multiple params and some are optional, you'll need to pass in `null` for |
| 258 | +those. |
0 commit comments