Sinch Engage: Archive and Acknowledge Delivery Reports
See more Sinch Engage Examples
Fetch one batch of up to 100 pending delivery reports, save the entire batch in a private local inbox, then acknowledge only those record IDs. Acknowledged records stop appearing in subsequent polls. Run again to drain further batches; this is a queue, not page-number pagination.
This example treats a checked local archive write as acceptance for later processing. A production inbox should commit durable, atomic storage and deduplicate by account plus delivery_report_id before applying business effects. A failed acknowledgement can cause redelivery. Coordinate consumers: polling covers the account and acknowledgements affect other pollers. Protect the archive because it contains personal data.
Request delivery_report=true when sending. Several reports can describe the same message: deduplicate by delivery_report_id, not message_id, and use event timestamps when applying changes. Reports may carry an additional charge.
Sinch Engage is the renamed MessageMedia platform. This uses the Engage API, not Sinch Conversation API credentials or endpoints. Create an Engage API key/secret and place {"api_key":"YOUR_KEY","api_secret":"YOUR_SECRET"} in private sinch-engage.json. Select the account's regional HTTPS host: https://au.app.api.sinch.com, https://eu.app.api.sinch.com, or https://us.app.api.sinch.com. Basic authentication here means the API key/secret over verified TLS. Do not ship account credentials in a public mobile, browser, or desktop application.
Transport success and API success are checked separately. On HTTP 429, honor Retry-After before a bounded retry. Check account permissions and regional credentials for 401/403. A timeout or 5xx response after submitting a write can leave its outcome uncertain: reconcile before retrying, especially sends and resource creation. Metadata is correlation data, not an idempotency key. Inspect error bodies privately because they can contain phone numbers and message content.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @iTmp2 int
DECLARE @iTmp3 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Store real credentials in private configuration, never in distributed client code.
DECLARE @config int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @config OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @config, 'Strict', 1
EXEC sp_OAMethod @config, 'LoadFile', @success OUT, 'sinch-engage.json'
IF @success <> 1
BEGIN
PRINT 'Could not load private sinch-engage.json.'
EXEC @hr = sp_OADestroy @config
RETURN
END
EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'api_key'
EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp1 OUT, 'api_key', '', 1
EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp2 OUT, 'api_secret'
EXEC sp_OAMethod @config, 'StringOfEquals', @iTmp3 OUT, 'api_secret', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1) or (@iTmp2 <> 1) or (@iTmp3 = 1)
BEGIN
PRINT 'The private configuration must contain api_key and api_secret.'
EXEC @hr = sp_OADestroy @config
RETURN
END
-- Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
DECLARE @apiHost nvarchar(4000)
SELECT @apiHost = 'https://au.app.api.sinch.com'
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'FollowRedirects', 0
EXEC sp_OASetProperty @http, 'ConnectTimeout', 20
EXEC sp_OASetProperty @http, 'ReadTimeout', 30
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'api_key'
EXEC sp_OASetProperty @http, 'Login', @sTmp0
EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'api_secret'
EXEC sp_OASetProperty @http, 'Password', @sTmp0
EXEC sp_OASetProperty @http, 'BasicAuth', 1
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'
DECLARE @url int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
DECLARE @result int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @result OUT
EXEC sp_OASetProperty @result, 'Strict', 1
EXEC sp_OAMethod @url, 'Clear', NULL
EXEC sp_OAMethod @url, 'Append', @success OUT, @apiHost
EXEC sp_OAMethod @url, 'Append', @success OUT, '/v1/delivery_reports'
EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @sTmp0, @resp
-- HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) and (@iTmp0 = 0)
BEGIN
PRINT 'HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@iTmp0 <> 200)
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Unexpected HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 429
BEGIN
EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
PRINT 'Retry-After: ' + @sTmp0
END
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Expected a JSON response.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
EXEC sp_OAMethod @result, 'SizeOfArray', @iTmp0 OUT, 'delivery_reports'
IF @iTmp0 < 0
BEGIN
PRINT 'Expected an array in the response.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
DECLARE @count int
EXEC sp_OAMethod @result, 'SizeOfArray', @count OUT, 'delivery_reports'
IF @count = 0
BEGIN
PRINT 'No pending records.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
IF @count > 100
BEGIN
PRINT 'Unexpected batch size; nothing was acknowledged.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
RETURN
END
DECLARE @confirmation int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @confirmation OUT
DECLARE @i int
SELECT @i = 0
WHILE (@i < @count)
BEGIN
EXEC sp_OASetProperty @result, 'I', @i
EXEC sp_OASetProperty @confirmation, 'I', @i
EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'delivery_reports[i].delivery_report_id'
EXEC sp_OAMethod @result, 'StringOfEquals', @iTmp1 OUT, 'delivery_reports[i].delivery_report_id', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'The response did not contain the expected ID.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @confirmation
RETURN
END
EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'delivery_reports[i].delivery_report_id'
EXEC sp_OAMethod @confirmation, 'UpdateString', @success OUT, 'delivery_report_ids[i]', @sTmp0
SELECT @i = @i + 1
END
-- Preserve the complete payload, including channel-specific and future fields.
DECLARE @fac int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
EXEC sp_OAMethod @fac, 'DirEnsureExists', @success OUT, 'engage-inbox/delivery_reports'
IF @success <> 1
BEGIN
PRINT 'Could not create the private inbox directory; no acknowledgement sent.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @confirmation
EXEC @hr = sp_OADestroy @fac
RETURN
END
DECLARE @hash int
EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @hash OUT
EXEC sp_OASetProperty @hash, 'HashAlgorithm', 'sha256'
EXEC sp_OASetProperty @hash, 'Charset', 'utf-8'
EXEC sp_OASetProperty @hash, 'EncodingMode', 'hex'
DECLARE @digest nvarchar(4000)
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @hash, 'HashStringENC', @digest OUT, @sTmp0
EXEC sp_OAGetProperty @hash, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Could not name the archive; no acknowledgement sent.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @confirmation
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @hash
RETURN
END
DECLARE @archive int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @archive OUT
EXEC sp_OAMethod @archive, 'Append', @success OUT, 'engage-inbox/delivery_reports/'
EXEC sp_OAMethod @archive, 'Append', @success OUT, @digest
EXEC sp_OAMethod @archive, 'Append', @success OUT, '.json'
EXEC sp_OAMethod @archive, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @result, 'WriteFile', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not save the records; no acknowledgement sent.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @confirmation
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @archive
RETURN
END
-- Acknowledge only after the checked archive write succeeds.
EXEC sp_OAMethod @url, 'Clear', NULL
EXEC sp_OAMethod @url, 'Append', @success OUT, @apiHost
EXEC sp_OAMethod @url, 'Append', @success OUT, '/v1/delivery_reports/confirmed'
EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', @sTmp0, @confirmation, 'application/json', @resp
-- HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) and (@iTmp0 = 0)
BEGIN
PRINT 'HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write.'
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @confirmation
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @archive
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@iTmp0 <> 202)
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Unexpected HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 429
BEGIN
EXEC sp_OAMethod @resp, 'GetHeaderField', @sTmp0 OUT, 'Retry-After'
PRINT 'Retry-After: ' + @sTmp0
END
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @confirmation
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @archive
RETURN
END
PRINT 'Archived and acknowledged records: ' + @count
EXEC @hr = sp_OADestroy @config
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @url
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @result
EXEC @hr = sp_OADestroy @confirmation
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @hash
EXEC @hr = sp_OADestroy @archive
END
GO