Dragon API

Direct HTTP Routing

You don't have to use dragon_api.lsl: any script can call the server itself with llHTTPRequest and read the answer in http_response. Dragon Script Routing is a thin layer over these calls, with the same fields and error codes.

  • Base URL: https://dragonaiconnect.com
  • Every call: POST with a JSON body (HTTP_MIMETYPE, "application/json"). Ask for long replies with HTTP_BODY_MAXLENGTH, 16384.
  • After linking: send the token in a header: HTTP_CUSTOM_HEADER, "X-Session-Token", token.
  • Answers: status 200 with JSON on success; otherwise a 4xx/5xx status with {"error", "code"}, using the codes in DRAGON_ERROR.

Caveats

  • The token is a password for your agent. Keep it in protected linkset data (llLinksetDataWriteProtected), never in chat, notecards or hover text, and delete it when the object changes owner (CHANGED_OWNER).
  • Make the device_id once with llGenerateKey() and keep it: linking again with the same ID keeps the same agent (name, personality, memory).
  • Second Life adds X-SecondLife-Owner-Key and friends to every request; the server uses them to match the object to its owner's account.
POST/v1/pair/startno token

Starts linking the object to its owner's account.

  • key device_id – required: this object's ID, made once with llGenerateKey().

Returns {"code", "poll_secret", "url", "expires_in"}. Send the owner to url (e.g. llLoadURL), then poll. The link expires after expires_in seconds (10 minutes).

POST/v1/pair/pollno token

Checks whether the owner has approved the link. Poll every 5 seconds.

  • string code – required, from pair/start.
  • string poll_secret – required, from pair/start.

Returns {"status": "pending"}, or {"status": "approved", "token", "device_id"}. The token is given once: store it straight away. Errors: PAIRING_EXPIRED (start again), PAIRING_CLAIMED, PAIRING_NOT_FOUND.

POST/v1/chattoken

Asks the AI. The body is the same JSON as DRAGON_REQUEST (prompt or messages required); the answer is the same JSON as DRAGON_REPLY.

INVALID_TOKEN, TOKEN_EXPIRED or DEVICE_REVOKED mean the link is gone: start pairing again with the same device_id.

POST/v1/heartbeattoken

Keeps the agent active and returns its status. Body: {}. Call it every few hours; agents with no contact for 7 days are paused.

Returns {"status", "server_time", "spent_usd", "limit_usd", "config"}, where config is the dashboard settings.

POST/v1/inboundtoken

Optional: lets the server push to your object (settings changes, Agent results). Get a URL with llRequestSecureURL(), then register it.

  • string url – required: the URL from http_request with URL_REQUEST_GRANTED.
  • string secret – required: 16–128 random characters you choose.

Pushes arrive in http_request as POST <url>?s=<secret> with {"text", "actions"} (up to 2 KB). Check llGetHTTPHeader(id, "x-query-string") == "s=" + secret, act on it, and answer with llHTTPResponse(id, 200, "ok"). URLs change when the region restarts or the object moves: register the new one each time.

POST/v1/pair/clonetoken

Makes a copy its own agent, with no browser. Call it from the copy, with the token it inherited.

  • key new_device_id – required: a fresh llGenerateKey().

Returns {"device_id", "token", "config"}: store both, replacing the old ones.

POST/v1/notecardtoken

Uploads a FAQ or Knowledge notecard. Body: same as DRAGON_NOTECARD.

Complete example: direct HTTP

One script that links itself and answers the owner, using only llHTTPRequest:

string SERVER = "https://dragonaiconnect.com";
string PASS   = "choose a long random password";   // protects the token in linkset data

string gDevice;
string gToken;
string gCode;
string gPollSecret;

key post(string path, string body)
{
    list opts = [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/json", HTTP_BODY_MAXLENGTH, 16384];
    if (gToken != "") opts += [HTTP_CUSTOM_HEADER, "X-Session-Token", gToken];
    return llHTTPRequest(SERVER + path, opts, body);
}

startLinking()
{
    gToken = "";
    llLinksetDataDeleteProtected("dragon_token", PASS);
    post("/v1/pair/start", llList2Json(JSON_OBJECT, ["device_id", gDevice]));
}

default
{
    state_entry()
    {
        gDevice = llLinksetDataRead("dragon_device");
        if (gDevice == "") {
            gDevice = (string)llGenerateKey();
            llLinksetDataWrite("dragon_device", gDevice);
        }
        gToken = llLinksetDataReadProtected("dragon_token", PASS);
        if (gToken == "") startLinking();
        llListen(0, "", llGetOwner(), "");
    }

    changed(integer change)
    {
        if (change & CHANGED_OWNER) { llLinksetDataReset(); llResetScript(); }
    }

    listen(integer channel, string name, key id, string msg)
    {
        if (gToken == "") return;
        post("/v1/chat", llList2Json(JSON_OBJECT, [
            "prompt", msg, "speaker", (string)id, "speaker_name", name, "max_chars", 1000]));
    }

    timer()
    {
        post("/v1/pair/poll", llList2Json(JSON_OBJECT, ["code", gCode, "poll_secret", gPollSecret]));
    }

    http_response(key request, integer status, list meta, string body)
    {
        string code = llJsonGetValue(body, ["code"]);
        if (llJsonGetValue(body, ["poll_secret"]) != JSON_INVALID) {            // pairing started
            gCode = code;
            gPollSecret = llJsonGetValue(body, ["poll_secret"]);
            llLoadURL(llGetOwner(), "Link this object to your account", llJsonGetValue(body, ["url"]));
            llSetTimerEvent(5.0);
        }
        else if (llJsonGetValue(body, ["status"]) == "approved") {              // owner approved
            gToken = llJsonGetValue(body, ["token"]);
            llLinksetDataWriteProtected("dragon_token", gToken, PASS);
            llSetTimerEvent(0.0);
            llOwnerSay("Linked!");
        }
        else if (status == 200 && llJsonGetValue(body, ["text"]) != JSON_INVALID)
            llSay(0, llJsonGetValue(body, ["text"]));                              // the AI's answer
        else if (code == "INVALID_TOKEN" || code == "TOKEN_EXPIRED" || code == "DEVICE_REVOKED"
                 || code == "PAIRING_EXPIRED") {
            llSetTimerEvent(0.0);
            startLinking();
        }
        else if (status != 200) llOwnerSay("AI error " + code + ": " + llJsonGetValue(body, ["error"]));
    }
}

This example skips the HTTP throttle, heartbeats and pushes. dragon_api.lsl handles all of them, so it's a good reference when building your own.