# get_balance

Returns the bot’s avatar L$ balance.

```csharp
string params = llDumpList2String([
  "action="  + "get_balance",
  "apikey="  + llEscapeURL(sbApiKey),
  "botname=" + llEscapeURL(sbBotName),
  "secret="  + llEscapeURL(sbBotAccessCode)
], "&");

httpReq = llHTTPRequest("https://api.mysmartbots.com/api/bot.html", [HTTP_METHOD,"POST"], params);
```

# Variables

The following table shows input values (you send them with the API call) and returned output values.

## Input basic parameters

| Variable | Required | Description |
|----------|----------|-------------|
| action   | yes      | = get_balance |
| apikey   | yes      | Your personal [developer's API key](/doc/developers-api-key-tw8bQj4LSJ) |
| botname  | yes      | Your bot's SL login |
| secret   | yes      | [Bot access code](https://www.mysmartbots.com/docs/Bot_access_code) of your bot |
| dataType | optional | Set to "json" to get JSON reply instead of URL-encoded string |
| custom   | optional | The custom data (string) to be passed back to caller script. This value will be returned back to the caller in HTTP response |

## Input

This command has **no additional input parameters**.

## Output

*(To be received in* `*http_response*` *LSL event, see* [*docs for details*](/doc/doing-http-api-calls-c6FSEMF7bP)*)*

| Variable | Description |
|----------|-------------|
| result   | OK - command completed successfully<br>FAIL - command failed |
| resulttext | Detailed reason for the failure |
| custom   | The value from input "custom" parameter. See above |
| balance  | The L$ balance of the bot |

# Example

This example script requests the bot's avatar balance from LSL and demonstrates how to parse HTTP API replies using the `get_post_value` function.

```csharp
string sbApiKey = "...";
string sbBotName = "OneSmartBot Resident";
string sbBotAccessCode = "...";

key httpReq = NULL_KEY;

// Get posted data's value.
string get_post_value(string content, string returns) {
    list params = llParseString2List(content, ["&"], []);
    integer index = ~llGetListLength(params);
    list keys;
    list values;

    while (++index) {
        list parsedParams = llParseString2List(llList2String(params, index), ["="], []);
        keys += llUnescapeURL(llList2String(parsedParams, 0));
        values += llUnescapeURL(llList2String(parsedParams, 1));
    }

    integer found = llListFindList(keys, [returns]);
    if (~found) {
        return llList2String(values, found);
    } else {
        return "";
    }
}

default {
    touch_start(integer total_number) {
        if (llDetectedKey(0) != llGetOwner()) {
            llInstantMessage(llDetectedKey(0), "Only my owner can use me!");
            return;
        }

        string params = llDumpList2String([
            "action="  + "get_balance",
            "apikey="  + llEscapeURL(sbApiKey),
            "botname=" + llEscapeURL(sbBotName),
            "secret="  + llEscapeURL(sbBotAccessCode)
        ], "&");

        httpReq = llHTTPRequest("https://api.mysmartbots.com/api/bot.html",
            [HTTP_METHOD, "POST"], params);
    }

    http_response(key request_id, integer status, list metadata, string body) {
        if (request_id != httpReq) return;

        string action     = get_post_value(body, "action");
        string result     = get_post_value(body, "result");
        string resulttext = get_post_value(body, "resulttext");
        string balance    = get_post_value(body, "balance");

        if (result == "FAIL") {
            llOwnerSay("Command '" + action + "' failed: " + resulttext);
        } else {
            llOwnerSay("Your bot's balance is L$" + balance);
        }
    }
}
```