Chilkat Online Tools

Node.js / ShipEngine Walkthrough / Add funds to a carrier

Back to Collection Items

var os = require('os');
if (os.platform() == 'win32') {  
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node21-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node21-win64'); 
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node21-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node21-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node21-linux64');
    }
} else if (os.platform() == 'darwin') {
    if (os.arch() == 'arm64') {
        var chilkat = require('@chilkat/ck-node21-mac-m1');
    } else {
        var chilkat = require('@chilkat/ck-node21-macosx');
    }
}


function chilkatExample() {

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    var http = new chilkat.Http();
    var success;

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "amount": 100,
    //   "currency": "usd"
    // }

    var json = new chilkat.JsonObject();
    json.UpdateInt("amount",100);
    json.UpdateString("currency","usd");

    http.SetRequestHeader("API-Key","{{API_KEY}}");
    http.SetRequestHeader("Content-Type","application/json");

    var sbRequestBody = new chilkat.StringBuilder();
    json.EmitSb(sbRequestBody);

    // resp: HttpResponse
    var resp = http.PTextSb("PUT","https://api.shipengine.com/v1/carriers/se-963358/add_funds",sbRequestBody,"utf-8","application/json",false,false);
    if (http.LastMethodSuccess == false) {
        console.log(http.LastErrorText);
        return;
    }

    var sbResponseBody = new chilkat.StringBuilder();
    resp.GetBodySb(sbResponseBody);

    var jResp = new chilkat.JsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.EmitCompact = false;

    console.log("Response Body:");
    console.log(jResp.Emit());

    var respStatusCode = resp.StatusCode;
    console.log("Response Status Code = " + respStatusCode);
    if (respStatusCode >= 400) {
        console.log("Response Header:");
        console.log(resp.Header);
        console.log("Failed.");

        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "balance": {
    //     "amount": 127.43,
    //     "currency": "usd"
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    var Amount = jResp.StringOf("balance.amount");
    var v_Currency = jResp.StringOf("balance.currency");

}

chilkatExample();

Curl Command

curl -X PUT
	-H "API-Key: {{API_KEY}}"
	-H "Content-Type: application/json"
	-d '{
	"amount": 100,
	"currency": "usd"
}'
https://api.shipengine.com/v1/carriers/se-963358/add_funds

Postman Collection Item JSON

{
  "name": "Add funds to a carrier",
  "request": {
    "method": "PUT",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n\t\"amount\": 100,\n\t\"currency\": \"usd\"\n}"
    },
    "url": {
      "raw": "https://api.shipengine.com/v1/carriers/{{stamps_com}}/add_funds",
      "protocol": "https",
      "host": [
        "api",
        "shipengine",
        "com"
      ],
      "path": [
        "v1",
        "carriers",
        "{{stamps_com}}",
        "add_funds"
      ]
    },
    "description": "You can check your balance for a particular carrier by calling List Carriers endpoint. For any carrier account where `requires_funded_amount` is true, you can add funds.\n\n> **NOTE:** This endpoint must be enabled by ShipEngine support, if you'd like to utilize this resource, please contact our team."
  },
  "response": [
    {
      "name": "Add funds to a carrier",
      "originalRequest": {
        "method": "PUT",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n\t\"amount\": 100,\n\t\"currency\": \"usd\"\n}"
        },
        "url": {
          "raw": "https://api.shipengine.com/v1/carriers/{{stamps_com}}/add_funds",
          "protocol": "https",
          "host": [
            "api",
            "shipengine",
            "com"
          ],
          "path": [
            "v1",
            "carriers",
            "{{stamps_com}}",
            "add_funds"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Tue, 17 Sep 2019 15:14:01 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "307"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "https://www.shipengine.com"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "x-shipengine-requestid",
          "value": "38af433e-6dee-41d8-b064-312e636760fe"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"balance\": {\n    \t\"amount\": 127.43,\n    \t\"currency\": \"usd\"\n    }\n}"
    }
  ]
}