Sample code for 30+ languages & platforms
SQL Server

CallRail API - Create an Outbound Phone Call

See more CallRail Examples

Initiates an outbound call from 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 POST \
    --      -H "Content-Type: application/json" \
    --      -d '{
    --           "caller_id": "+17703334455",
    --           "business_phone_number": "+14045556666",
    --           "customer_phone_number": "+14044442233",
    --           "recording_enabled": true,
    --           "outbound_greeting_recording_url": "http://www.test.com/greeting.mp3",
    --           "outbound_greeting_text": "These are not the droids you are looking for."
    --          }' \
    --          "https://api.callrail.com/v3/a/{account_id}/calls.json"

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

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

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

    -- {
    --   "caller_id": "+17703334455",
    --   "business_phone_number": "+14045556666",
    --   "customer_phone_number": "+14044442233",
    --   "recording_enabled": true,
    --   "outbound_greeting_recording_url": "http://www.test.com/greeting.mp3",
    --   "outbound_greeting_text": "These are not the droids you are looking for."
    -- }

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'caller_id', '+17703334455'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'business_phone_number', '+14045556666'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'customer_phone_number', '+14044442233'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'recording_enabled', 1
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'outbound_greeting_recording_url', 'http://www.test.com/greeting.mp3'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'outbound_greeting_text', 'These are not the droids you are looking for.'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Authorization', 'Token token={api_token}'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://api.callrail.com/v3/a/{account_id}/calls.json', @json, 'application/json', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

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

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
    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 @resp, 'StatusCode', @respStatusCode OUT

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

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @resp
        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": null,
    --   "business_phone_number": "+19044567899",
    --   "customer_city": "Atlanta",
    --   "customer_country": "US",
    --   "customer_name": null,
    --   "customer_phone_number": "+14703444700",
    --   "customer_state": "GA",
    --   "direction": "outbound",
    --   "duration": null,
    --   "id": "CAL8154748ae6bd4e278a7cddd38a662f4f",
    --   "recording": null,
    --   "recording_duration": null,
    --   "start_time": "2017-02-22T15:02:24.916-05:00",
    --   "tracking_phone_number": "+19044567899",
    --   "voicemail": false
    -- }

    -- 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 @answered nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @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 nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @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 @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'

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


END
GO