Creating new bot from template using API

Hi,

Is it possible to create a BotPress bot entirely using the API? More specifically, can I accomplish most of all of what I would do in the UI just using API methods? I looked, and I don’t see a createBot() method, so I’m wondering if this is possible.

Second question, I read that multi-tenant support has been added in version 11. Is there any documentation on how this works?

Thanks!

1 Like

Hi!
You may try to check network panel in devtools, when you create bot.

It sends post-request to /api/v1/admin/bots with {“id”:“qweqwe”,“name”:“qweqwe”,“template”:{“id”:“welcome-bot”,“moduleId”:“builtin”}} body.
You may use axios to send authorized request.

As for multi-tenant - not sure, how to achieve it. Fast search navigates me to https://github.com/botpress/botpress/pull/891, which have this description:

" This functionality enables true multi-tenant bots, enabled by new pages config setting that overrides any defaults, per page ID. The code works as it did before if these configs are not provided, and also works if ALL configs are moved under page IDs"

Thanks for your reply,
how i can authorize the request as i tried to post a request using fiddler but got this error

also how to know the token that should be send in the request.

{“statusCode”:401,“errorCode”:“BP_0041”,“type”:“UnauthorizedError”,“message”:“Unauthorized: Authorization header is missing”,“docs”:“https://botpress.io/docs",“showStackInDev”:true,“stack”:"Error: Unauthorized: Authorization header is missing\n at Object. (C:\Users\sales_com\Downloads\botpress-master\out\bp\core\routers\util.js:60:21)\n at Generator.next ()\n at C:\Users\sales_com\Downloads\botpress-master\out\bp\core\routers\util.js:7:71\n at Promise._execute (C:\Users\sales_com\Downloads\botpress-master\modules\nlu\node_modules\bluebird\js\release\debuggability.js:313:9)\n at Promise._resolveFromExecutor (C:\Users\sales_com\Downloads\botpress-master\modules\nlu\node_modules\bluebird\js\release\promise.js:488:18)\n at new Promise (C:\Users\sales_com\Downloads\botpress-master\modules\nlu\node_modules\bluebird\js\release\promise.js:79:10)\n at __awaiter (C:\Users\sales_com\Downloads\botpress-master\out\bp\core\routers\util.js:3:12)\n at C:\Users\sales_com\Downloads\botpress-master\out\bp\core\routers\util.js:58:75\n at Layer.handle [as handle_request] (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\layer.js:95:5)\n at trim_prefix (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:317:13)\n at C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:284:7\n at Function.process_params (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:335:12)\n at next (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:275:10)\n at Function.handle (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:174:3)\n at router (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:47:12)\n at Layer.handle [as handle_request] (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\layer.js:95:5)\n at trim_prefix (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:317:13)\n at C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:284:7\n at Function.process_params (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:335:12)\n at next (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:275:10)\n at C:\Users\sales_com\Downloads\botpress-master\node_modules\express-urlrewrite\index.js:42:16\n at Layer.handle [as handle_request] (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\layer.js:95:5)\n at trim_prefix (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:317:13)\n at C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:284:7\n at Function.process_params (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:335:12)\n at next (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:275:10)\n at cors (C:\Users\sales_com\Downloads\botpress-master\node_modules\cors\lib\index.js:188:7)\n at C:\Users\sales_com\Downloads\botpress-master\node_modules\cors\lib\index.js:224:17\n at originCallback (C:\Users\sales_com\Downloads\botpress-master\node_modules\cors\lib\index.js:214:15)\n at C:\Users\sales_com\Downloads\botpress-master\node_modules\cors\lib\index.js:219:13\n at optionsCallback (C:\Users\sales_com\Downloads\botpress-master\node_modules\cors\lib\index.js:199:9)\n at corsMiddleware (C:\Users\sales_com\Downloads\botpress-master\node_modules\cors\lib\index.js:204:7)\n at Layer.handle [as handle_request] (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\layer.js:95:5)\n at trim_prefix (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:317:13)\n at C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:284:7\n at Function.process_params (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:335:12)\n at next (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:275:10)\n at urlencodedParser (C:\Users\sales_com\Downloads\botpress-master\node_modules\body-parser\lib\types\urlencoded.js:100:7)\n at app.use (C:\Users\sales_com\Downloads\botpress-master\out\bp\core\server.js:215:73)\n at Layer.handle [as handle_request] (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\layer.js:95:5)\n at trim_prefix (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:317:13)\n at C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:284:7\n at Function.process_params (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:335:12)\n at next (C:\Users\sales_com\Downloads\botpress-master\node_modules\express\lib\router\index.js:275:10)\n at jsonParser (C:\Users\sales_com\Downloads\botpress-master\node_modules\body-parser\lib\types\json.js:119:7)\n at app.use (C:\Users\sales_com\Downloads\botpress-master\out\bp\core\server.js:207:76)”,“full”:“Unauthorized: Authorization header is missing”}

You need to provide the "Authorization" header, which contains your personal API Key (the token expires after X hours, as defined in your botpress.config.json file). You can retrieve your API Key from the chrome console while on the Botpress UI as follow:

i tried to call /api/v1/auth/login/basic/default and got
{“status”:“success”,“message”:“Login successful”,“payload”:{“token”:“eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Imt0aGFyd2F0QHByZW1pdGVrLmNvbSIsInN0cmF0ZWd5IjoiZGVmYXVsdCIsImlzU3VwZXJBZG1pbiI6dHJ1ZSwiaWF0IjoxNTc5MTAwMjQ0LCJleHAiOjE1NzkxMjE4NDQsImF1ZCI6ImNvbGxhYm9yYXRvcnMifQ.rscyfoVPsMZdTDkMzbU8t02f7l0d1vNiR2gOOCOt1j8”}}

but when tried to use this token in /api/v1/admin/bots
i got the error “Unauthorized”

Here is my C# function to create bots using API

protected APIResponse CreateBot()
{
APIResponse apiResponse = new APIResponse();
string url = “http://<my_domain>/api/v1/admin/bots”;
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = “application/json”;
httpWebRequest.Method = “POST”;
httpWebRequest.Headers.Add(“Authorization”, @“bearer {” + token + “}”);

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"id\":\"test1\",\"name\":\"test1\",\"template\":{\"id\":\"empty-bot\",\"moduleId\":\"builtin\"}}";
            streamWriter.Write(json);
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            apiResponse = new JavaScriptSerializer().Deserialize<APIResponse>(result);
        }
        return apiResponse;
    }

plz tell me what may be missing in my code