Sample code for 30+ languages & platforms
Lua

GetHarvest - Get Client Contacts

See more GetHarvest Examples

Returns a list of your contacts.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local http = chilkat.Http.new()

-- Implements the following CURL command:

-- curl "https://api.harvestapp.com/v2/contacts" \
--   -H "Authorization: Bearer ACCESS_TOKEN" \
--   -H "Harvest-Account-Id: ACCOUNT_ID" \
--   -H "User-Agent: MyApp (yourname@example.com)"

http:set_request_header("User-Agent", "MyApp (yourname@example.com)")
http:set_request_header("Authorization", "Bearer ACCESS_TOKEN")
http:set_request_header("Harvest-Account-Id", "ACCOUNT_ID")

local sbResponseBody = chilkat.StringBuilder.new()
local success = http:quick_get_sb("https://api.harvestapp.com/v2/contacts", sbResponseBody)
if not success then
    print(http.last_error_text)
    return
end

local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false

print("Response Body:")
print(jResp:emit())

local respStatusCode = http.last_status
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
    print("Response Header:")
    print(http.last_header)
    print("Failed.")
    return
end

-- Sample JSON response:

-- {
--   "contacts": [
--     {
--       "id": 4706479,
--       "title": "Owner",
--       "first_name": "Jane",
--       "last_name": "Doe",
--       "email": "janedoe@example.com",
--       "phone_office": "(203) 697-8885",
--       "phone_mobile": "(203) 697-8886",
--       "fax": "(203) 697-8887",
--       "created_at": "2017-06-26T21:20:07Z",
--       "updated_at": "2017-06-26T21:27:07Z",
--       "client": {
--         "id": 5735774,
--         "name": "ABC Corp"
--       }
--     },
--     {
--       "id": 4706453,
--       "title": "Manager",
--       "first_name": "Richard",
--       "last_name": "Roe",
--       "email": "richardroe@example.com",
--       "phone_office": "(318) 515-5905",
--       "phone_mobile": "(318) 515-5906",
--       "fax": "(318) 515-5907",
--       "created_at": "2017-06-26T21:06:55Z",
--       "updated_at": "2017-06-26T21:27:20Z",
--       "client": {
--         "id": 5735776,
--         "name": "123 Industries"
--       }
--     }
--   ],
--   "per_page": 100,
--   "total_pages": 1,
--   "total_entries": 2,
--   "next_page": null,
--   "previous_page": null,
--   "page": 1,
--   "links": {
--     "first": "https://api.harvestapp.com/v2/contacts?page=1&per_page=100",
--     "next": null,
--     "previous": null,
--     "last": "https://api.harvestapp.com/v2/contacts?page=1&per_page=100"
--   }
-- }

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

local id = 0
local title = ""
local first_name = ""
local last_name = ""
local email = ""
local phone_office = ""
local phone_mobile = ""
local fax = ""
local created_at = ""
local updated_at = ""
local clientId = 0
local clientName = ""

local per_page = jResp:int_of("per_page")
local total_pages = jResp:int_of("total_pages")
local total_entries = jResp:int_of("total_entries")
local next_page = jResp:string_of("next_page")
local previous_page = jResp:string_of("previous_page")
local page = jResp:int_of("page")
local linksFirst = jResp:string_of("links.first")
local linksNext = jResp:string_of("links.next")
local linksPrevious = jResp:string_of("links.previous")
local linksLast = jResp:string_of("links.last")
local i = 0
local count_i = jResp:size_of_array("contacts")
while i < count_i do
    jResp.i = i
    id = jResp:int_of("contacts[i].id")
    title = jResp:string_of("contacts[i].title")
    first_name = jResp:string_of("contacts[i].first_name")
    last_name = jResp:string_of("contacts[i].last_name")
    email = jResp:string_of("contacts[i].email")
    phone_office = jResp:string_of("contacts[i].phone_office")
    phone_mobile = jResp:string_of("contacts[i].phone_mobile")
    fax = jResp:string_of("contacts[i].fax")
    created_at = jResp:string_of("contacts[i].created_at")
    updated_at = jResp:string_of("contacts[i].updated_at")
    clientId = jResp:int_of("contacts[i].client.id")
    clientName = jResp:string_of("contacts[i].client.name")
    i = i + 1
end