Sample code for 30+ languages & platforms
SQL Server

CallRail API - Retrieving a Single Call

See more CallRail Examples

Returns a single call object associated with the target account.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Implements the following CURL command:

    -- curl -H "Authorization: Token token={api_token}" \
    --      -X GET \
    --      "https://api.callrail.com/v3/a/{account_id}/calls/{call_id}.json?fields=keywords_spotted"

    -- Use the following online tool to generate HTTP code from a CURL command
    -- Convert a cURL Command to HTTP Source Code

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Authorization', 'Token token={api_token}'

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://api.callrail.com/v3/a/{account_id}/calls/{call_id}.json?fields=keywords_spotted', @sbResponseBody
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @http, 'LastStatus', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END

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

    -- {
    --   "answered": false,
    --   "business_phone_number": null,
    --   "customer_city": "New York City",
    --   "customer_country": "US",
    --   "customer_name": "Jimmy Pesto, Sr.",
    --   "customer_phone_number": "+13036231131",
    --   "customer_state": "NY",
    --   "direction": "inbound",
    --   "duration": 4,
    --   "id": "CAL8154748ae6bd4e278a7cddd38a662f4f",
    --   "recording": "https://api.callrail.com/v3/a/227799611/calls/111222333/recording.json",
    --   "recording_duration": "27",
    --   "recording_player": "https://app.callrail.com/calls/111222333/recording?access_key=3b91eb7f7cc08a4d01ed",
    --   "start_time": "2017-01-24T11:27:48.119-05:00",
    --   "tracking_phone_number": "+13038163491",
    --   "voicemail": false,
    --   "agent_email": "gil@televised.com",
    --   "keywords_spotted": [
    --     {
    --       "keyword": "test phrase one",
    --       "locations": [
    --         {
    --           "speaker": "caller",
    --           "start": 7.71
    --         },
    --         {
    --           "speaker": "agent",
    --           "start": 13.38
    --         },
    --         {
    --           "speaker": "caller",
    --           "start": 17.96
    --         }
    --       ]
    --     },
    --     {
    --       "keyword": "test phrase two",
    --       "locations": [
    --         {
    --           "speaker": "caller",
    --           "start": 8.44
    --         }
    --       ]
    --     }
    --   ]
    -- }

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

    DECLARE @keyword nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @speaker nvarchar(4000)

    DECLARE @start nvarchar(4000)

    DECLARE @answered int
    EXEC sp_OAMethod @jResp, 'BoolOf', @answered OUT, 'answered'
    DECLARE @business_phone_number nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @business_phone_number OUT, 'business_phone_number'
    DECLARE @customer_city nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @customer_city OUT, 'customer_city'
    DECLARE @customer_country nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @customer_country OUT, 'customer_country'
    DECLARE @customer_name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @customer_name OUT, 'customer_name'
    DECLARE @customer_phone_number nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @customer_phone_number OUT, 'customer_phone_number'
    DECLARE @customer_state nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @customer_state OUT, 'customer_state'
    DECLARE @direction nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @direction OUT, 'direction'
    DECLARE @duration int
    EXEC sp_OAMethod @jResp, 'IntOf', @duration OUT, 'duration'
    DECLARE @id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @id OUT, 'id'
    DECLARE @recording nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @recording OUT, 'recording'
    DECLARE @recording_duration nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @recording_duration OUT, 'recording_duration'
    DECLARE @recording_player nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @recording_player OUT, 'recording_player'
    DECLARE @start_time nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @start_time OUT, 'start_time'
    DECLARE @tracking_phone_number nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @tracking_phone_number OUT, 'tracking_phone_number'
    DECLARE @voicemail int
    EXEC sp_OAMethod @jResp, 'BoolOf', @voicemail OUT, 'voicemail'
    DECLARE @agent_email nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @agent_email OUT, 'agent_email'
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'keywords_spotted'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'StringOf', @keyword OUT, 'keywords_spotted[i].keyword'
        SELECT @j = 0
        EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_j OUT, 'keywords_spotted[i].locations'
        WHILE @j < @count_j
          BEGIN
            EXEC sp_OASetProperty @jResp, 'J', @j
            EXEC sp_OAMethod @jResp, 'StringOf', @speaker OUT, 'keywords_spotted[i].locations[j].speaker'
            EXEC sp_OAMethod @jResp, 'StringOf', @start OUT, 'keywords_spotted[i].locations[j].start'
            SELECT @j = @j + 1
          END
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO