Sample code for 30+ languages & platforms
SQL Server

Square API - Create Catalog Image

See more Square Examples

Uploads an image file to be represented by an CatalogImage object linked to an existing CatalogObject instance.

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 https://connect.squareup.com/v2/catalog/images \
    --   -X POST \
    --   -H 'Square-Version: 2020-07-22' \
    --   -H 'Authorization: Bearer ACCESS_TOKEN' \
    --   -H 'Accept: application/json' \
    --   -F 'file=@/local/path/to/file.jpg' \
    --   -F 'request={
    --     "idempotency_key": "528dea59-7bfb-43c1-bd48-4a6bba7dd61f86",
    --     "object_id": "ND6EA5AAJEO5WL3JNNIAQA32",
    --     "image": {
    --       "id": "#TEMP_ID",
    --       "type": "IMAGE",
    --       "image_data": {
    --         "caption": "A picture of a cup of coffee"
    --       }
    --     }
    --   }'

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

    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT

    EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
    EXEC sp_OASetProperty @req, 'Path', '/v2/catalog/images'
    EXEC sp_OASetProperty @req, 'ContentType', 'multipart/form-data'
    EXEC sp_OAMethod @req, 'AddFileForUpload2', @success OUT, 'file', '/local/path/to/file.jpg', 'image/jpeg'

    -- Make sure to use an object_id for an already-existing catalog item.
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'idempotency_key', '528dea59-7bfb-43c1-bd48-4a6bba7dd61f86'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'object_id', '2PTZYUOFGGDMT75UZLHQAPAC'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'image.id', '#TEMP_ID'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'image.type', 'IMAGE'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'image.image_data.caption', 'A picture of a cup of coffee'

    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'request', @sTmp0

    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Expect', '100-continue'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Authorization', 'Bearer ACCESS_TOKEN'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Accept', 'application/json'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Square-Version', '2020-07-22'

    -- This example uses the sandbox: connect.squareupsandbox.com
    -- Production should use connect.squareup.com
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpSReq', @success OUT, 'connect.squareupsandbox.com', 443, 1, @req, @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        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 @req
        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)

    -- {
    --   "image": {
    --     "type": "IMAGE",
    --     "id": "UBXKMBQ4QK47QXMNQD6ELCZT",
    --     "updated_at": "2020-08-07T15:20:09.779Z",
    --     "version": 1596813609779,
    --     "is_deleted": false,
    --     "present_at_all_locations": true,
    --     "image_data": {
    --       "name": "",
    --       "url": "https://square-catalog-sandbox.s3.amazonaws.com/files/168973eab6f8b39b5cbdad52e3f82b23be196e2b/original.jpeg",
    --       "caption": "A picture of a cup of coffee"
    --     }
    --   }
    -- }

    -- 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 @imageType nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @imageType OUT, 'image.type'
    DECLARE @imageId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @imageId OUT, 'image.id'
    DECLARE @imageUpdated_at nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @imageUpdated_at OUT, 'image.updated_at'
    DECLARE @imageVersion int
    EXEC sp_OAMethod @jResp, 'IntOf', @imageVersion OUT, 'image.version'
    DECLARE @imageIs_deleted int
    EXEC sp_OAMethod @jResp, 'BoolOf', @imageIs_deleted OUT, 'image.is_deleted'
    DECLARE @imagePresent_at_all_locations int
    EXEC sp_OAMethod @jResp, 'BoolOf', @imagePresent_at_all_locations OUT, 'image.present_at_all_locations'
    DECLARE @imageImage_dataName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @imageImage_dataName OUT, 'image.image_data.name'
    DECLARE @imageImage_dataUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @imageImage_dataUrl OUT, 'image.image_data.url'
    DECLARE @imageImage_dataCaption nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @imageImage_dataCaption OUT, 'image.image_data.caption'

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


END
GO