{"id":20,"date":"2017-01-20T16:00:57","date_gmt":"2017-01-20T16:00:57","guid":{"rendered":"http:\/\/www.cleverbot.com\/payments\/?page_id=20"},"modified":"2018-07-24T18:52:32","modified_gmt":"2018-07-24T18:52:32","slug":"howto","status":"publish","type":"page","link":"https:\/\/www.cleverbot.com\/api\/howto\/","title":{"rendered":"How to use the Cleverbot API"},"content":{"rendered":"<p>The Cleverbot API\u00a0is RESTful. You call a URL on our server and we return JSON.<\/p>\n<p><strong>Sign up<\/strong><br \/>\nFirst of all, you need to sign up to our API. You will have to provide card details, and will be charged according to the package you choose, but you can cancel any time. Then visit <a href=\"\/payments\/my-account\/\">My Account<\/a>\u00a0where you will find your API key and usage statistics.<\/p>\n<p><strong>Making requests<br \/>\n<\/strong>Once you have your API key you can test the API straight away in your browser. Visit:<br \/>\n<em>https:\/\/www.cleverbot.com\/getreply?key=YOUR_API_KEY<\/em><\/p>\n<p>This will return a JSON object which looks like:<\/p>\n<pre>{\r\n  \"cs\":\"76nxdxIJO2...AAA\",\r\n  \"interaction_count\":\"1\",\r\n  \"input\":\"\",\r\n  \"output\":\"Good afternoon.\",\r\n  \"conversation_id\":\"AYAR1E3ITW\",\r\n  ...\r\n}<\/pre>\n<p><strong>Parameters<\/strong><br \/>\nThe URL for Cleverbot API should\u00a0always start with <em>http:\/\/www.cleverbot.com\/getreply<\/em> or\u00a0<em>https:\/\/www.cleverbot.com\/getreply<\/em>.<\/p>\n<p>The\u00a0<em>key<\/em> parameter is always required. It is your API key.<\/p>\n<p>The\u00a0<em>input<\/em> parameter is what you want to say to Cleverbot, such as &#8220;hello&#8221;.<\/p>\n<p>The\u00a0<em>cs<\/em> parameter stands for &#8220;cleverbot state&#8221;. It is the encoded state of the conversation so far and includes the whole conversation history up to that point. Every time you call the API, it will send back the <em>cs<\/em> parameter. You just have to send it right back again to continue the conversation.<\/p>\n<p>The\u00a0<em>callback<\/em> parameter allows for JSONP requests. The returned JSON object will be wrapped by the\u00a0<em>callback<\/em> function.<\/p>\n<p>For example, you can call:<br \/>\n<em>http:\/\/www.cleverbot.com\/getreply?key=YOUR_API_KEY&amp;input=Hello&amp;cs=76nxdxIJ02AAA&amp;callback=ProcessReply<\/em><\/p>\n<p>And it will reply something like this:<\/p>\n<pre>{\r\n  \"cs\":\"112nxdyIJO2...AAA\",\r\n  \"interaction_count\":\"2\",\r\n  \"input\":\"Hello.\",\r\n  \"output\":\"How are you?\",\r\n  \"conversation_id\":\"AYAR1E3ITW\",\r\n  \"interaction_1\": \"Hello.\",\r\n  \"interaction_1_other\": \"How are you?\",\r\n  \"interaction_2\": \"\",\r\n  \"interaction_2_other\": \"Good afternoon.\",\r\n  ...\r\n}<\/pre>\n<p><em>You do not need to pass in any other parameters.<\/em> The conversation history is contained within the <em>cs<\/em> variable.<\/p>\n<p>However, if you wish to override the conversation history, you can pass in variables like <em>vtext2<\/em> to override the last thing the bot said, <em>vtext3<\/em> for the previous thing the user said, and so on. This feature was added in mid May 2017.<\/p>\n<p><strong>New Feature for October 2017!<\/strong><\/p>\n<p>Along with the parameters above you can now also pass in <em>cb_settings_tweak1<\/em>, <em>cb_settings_tweak2<\/em> or <em>cb_settings_tweak3<\/em> to vary how Cleverbot will respond. These settings correspond to the tweaks menu item in the blue bar at the top of <a href=\"https:\/\/www.cleverbot.com\/\">www.cleverbot.com<\/a> (available only after you have signed up and logged in). The parameters are percentage values between 0 and 100. Tweak 1 varies Cleverbot&#8217;s reply from sensible to wacky, tweak 2 from shy to talkative and tweak 3 from self-centred to attentive.<\/p>\n<p><strong>JSON Reply<\/strong><br \/>\nThe JSON reply includes many variables. They are the same variables\u00a0as used by our\u00a0<a href=\"http:\/\/www.cleverscript.com\/\">Cleverscript<\/a>\u00a0product, which allows for scripted conversations to be placed on top of a mini-Cleverbot. The full list of these variables is shown and explained on <a href=\"http:\/\/www.cleverscript.com\/CSL\/CleverScriptManual.pdf\">page 26 of the Cleverscript manual<\/a>, but only a few are relevant to the Cleverbot API:<\/p>\n<ul>\n<li><em>cs<\/em>: state of the conversation so far, which contains an encoded copy of the conversation id and history<\/li>\n<li><em>interaction_count<\/em>: how many pairs of bot\/user interactions have occurred so far<\/li>\n<li><em>input<\/em>: the entire user input, with any spaces trimmed off both ends<\/li>\n<li><em>output<\/em>: Cleverbot&#8217;s reply<\/li>\n<li><em>conversation_id<\/em>: identifier for this conversation between user and bot<\/li>\n<li><em>errorline<\/em>: any error information from Cleverbot, this is different from general the general errors described below<\/li>\n<li><em>time_taken<\/em>: the number of milliseconds the bot took to respond<\/li>\n<li><em>time_elapsed<\/em>: approximate number of seconds since conversation started<\/li>\n<li><em>interaction_1<\/em> to <em>interaction_100<\/em>: record of the previous interactions<\/li>\n<li><em>callback<\/em>: used by some versions of the software to store a callback function<\/li>\n<\/ul>\n<p>The\u00a0<em>interaction<\/em> variables come in pairs. For example\u00a0<em>interaction_1<\/em> contains the last thing the user said, and <em>interaction_1_other<\/em> is the bot&#8217;s reply.\u00a0<em>interaction_2<\/em> was the user&#8217;s previous input and so on. So to read the whole conversation in order, you have to display the interaction variables in reverse order, eg\u00a0<em>interaction5<\/em>, <em>interaction_5_other<\/em>, <em>interaction_4<\/em>, <em>interaction_4_other<\/em>, etc.<\/p>\n<p>Note that if a variable is blank, it may not be returned.<\/p>\n<p>Out of all of these variables only the <em>cs<\/em> variable need to be passed back to the API on the next request. It\u00a0contains the entire conversation history.<\/p>\n<p><strong>Status codes<br \/>\n<\/strong>If the request was successful, it will return the JSON format described above with an HTTP status code of 200. If there was an error, it will return a much smaller bit of JSON containing just <em>status<\/em>\u00a0and\u00a0<em>error<\/em>. Possible errors are:<\/p>\n<ul>\n<li>401: unauthorised due to missing or invalid API key or POST request, the Cleverbot API only accepts GET requests<\/li>\n<li>404: API not found<\/li>\n<li>413 or 414: request too large if you send a request over 64Kb<\/li>\n<li>502 or 504: unable to get reply from API server, please contact us<\/li>\n<li>503: too many requests from a single IP address or API key<\/li>\n<\/ul>\n<p><strong>Using the API<\/strong><br \/>\nOur API returns results in JSON format. Using PHP, a call to our API would look like:<\/p>\n<pre>&lt;?php\r\n$url = \"https:\/\/www.cleverbot.com\/getreply\";\r\n$key = \"YOUR_API_KEY\";\r\n$input = rawurlencode (\"How are you?\");\r\n$apidata = json_decode (file_get_contents (\"$url?input=$input&amp;key=$key\"));\r\n?&gt;<\/pre>\n<p>Or you can use Javascript and pass in a callback parameter. The following code snippet creates a\u00a0<em>&lt;script&gt;<\/em>\u00a0tag referencing our API. When the API returns the results, they will be passed to the\u00a0<em>ProcessReply<\/em>\u00a0function which will tell you the proportion\u00a0of love:<\/p>\n<pre>&lt;script&gt;\r\nurl = \"https:\/\/www.cleverbot.com\/getreply\";\r\nkey = \"YOUR_API_KEY\";\r\ninput = encodeURIComponent (\"How are you?\");\r\nfunction ProcessReply (data) {\r\n    if (data.error) console.log ('Error: ' + data.error);\r\n    else alert ('Reply: ' + data.output);\r\n}\r\nscript_element = document.createElement('script'); \/\/create new script element\r\nscript_element.src = url + \"?input=\" + input + '&amp;key=' + key + '&amp;callback=ProcessReply';\r\ndocument.getElementsByTagName ('head')[0].appendChild(script_element); \/\/append to page, which executes it\r\n&lt;\/script&gt;<\/pre>\n<p>The last three lines are much easier to read using\u00a0jQuery:<\/p>\n<pre>&lt;script&gt;\r\n$.ajax({\r\n    url: url,\r\n    data: {\"input\": input, \"key\": key}, \/\/input automatically encoded\r\n    dataType: \"jsonp\",\r\n    success: ProcessReply\r\n});\r\n&lt;\/script&gt;<\/pre>\n<p>Please note that putting your API key inside a Javascript file like this is not recommended on public websites. Your API key could be easily\u00a0used by someone else. So for public websites we\u00a0recommend making calls on the server-side using a language like Python or PHP.<\/p>\n<p>You can also use PHP to communicate with the API but Javascript to handle the result:<\/p>\n<pre>&lt;?php\r\n$url = \"https:\/\/www.cleverbot.com\/getreply\";\r\n$key = \"YOUR_API_KEY\";\r\n$input = rawurlencode (\"How are you?\");\r\necho \"&lt;script&gt;ProcessReply (\" . file_get_contents (\"$url?input=$input&amp;key=$key\") . \")&lt;\/script&gt;\";\r\n?&gt;<\/pre>\n<p><strong>Charging<\/strong><br \/>\nAccess to our API is provided on a monthly basis, starting on the day you place\u00a0your order. If you have chosen the API Starter the first three months are free.<\/p>\n<p>Each call to one of our API \u00a0containing your key counts as one API call. If you approach your monthly limit, we will contact you to let you know and ask you to upgrade to a different plan.<\/p>\n<p><strong>Changing plan<\/strong><br \/>\nYou can change your plan by going to <a href=\"\/api\/my-account\/\">My Account<\/a>, then <a href=\"\/api\/my-account\/subscriptions\/\">Subscriptions<\/a>, clicking\u00a0on your order to view it and pressing &#8220;Upgrade or Downgrade&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Cleverbot API\u00a0is RESTful. You call a URL on our server and we return JSON. Sign up First of all, you need to sign up to our API. You will have to provide card details, and will be charged according to the package you choose, but you can cancel any time. Then visit My Account\u00a0where &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.cleverbot.com\/api\/howto\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to use the Cleverbot API&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.cleverbot.com\/api\/wp-json\/wp\/v2\/pages\/20"}],"collection":[{"href":"https:\/\/www.cleverbot.com\/api\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.cleverbot.com\/api\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.cleverbot.com\/api\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cleverbot.com\/api\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":23,"href":"https:\/\/www.cleverbot.com\/api\/wp-json\/wp\/v2\/pages\/20\/revisions"}],"predecessor-version":[{"id":19740,"href":"https:\/\/www.cleverbot.com\/api\/wp-json\/wp\/v2\/pages\/20\/revisions\/19740"}],"wp:attachment":[{"href":"https:\/\/www.cleverbot.com\/api\/wp-json\/wp\/v2\/media?parent=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}