curlsmartsheet-api

Smartsheet API cURL - Attach URL to Sheet/Row


I want to attach a simple URL to a sheet or row in a sheet using cURL API. The Smartsheet documentation only explains how to attach a file. I have tried with a request body and adjusting the headers to somehow attach a URL but failed as I don't know how to adjust the headers or compose the body.

From Smartsheet API Docs:

Attach File or URL to Sheet Attaches a file to the sheet. The URL can be any of the following:
Normal URL (attachmentType "LINK")
Box.com URL (attachmentType "BOX_COM")
Dropbox URL (attachmentType "DROPBOX")

curl https://api.smartsheet.com/2.0/sheets/{sheetId}/attachments \
-H "Authorization: Bearer JKlMNOpQ12RStUVwxYZAbcde3F5g6hijklM789" \
-H "Content-Type: application/msword" \
-H 'Content-Disposition: attachment; filename="ProgressReport.docx"' \
-H "Content-Length: FILE_SIZE" \
-X POST \
--data-binary @ProgressReport.docx

Solution

  • Whenever I'm trying to solve a problem like the one you've described, I often find it helpful to look at how the item I'm trying to create is represented in a "Get item" API response -- then I can try to use that same data structure/contents in a "Create item" API request...99% of the time this approach is successful. I'll explain this process in the context of answering your question.

    First, I logged into the Smartsheet web app and added a URL attachment to a row.

    add URL attachment

    Next, I sent a List Attachments API request, so I could see the properties that got set for URL attachment when I added it via the web app.

    List Attachments request:

    GET https://api.smartsheet.com/2.0/sheets/2702602705784708/attachments
    

    List Attachments response:

    {
        "pageNumber": 1,
        "pageSize": 100,
        "totalPages": 1,
        "totalCount": 1,
        "data": [
            {
                "id": 41677365661572,
                "name": "Google",
                "url": "https://www.google.com",
                "attachmentType": "LINK",
                "parentType": "ROW",
                "parentId": 409928880836484,
                "createdAt": "2023-11-03T18:15:57Z",
                "createdBy": {
                    "name": "Kim Brandl",
                    "email": "*****@gmail.com"
                }
            }
        ]
    }
    

    Of the properties present in this API response, only 3 of them actually describe the URL attachment itself:

    Now that I know what properties get set for a URL Attachment (that's added via the app), I'm going to try issuing an Attach URL API request that sets those 3 properties for the URL I want to attach to the sheet.

    Attach URL (to sheet) request:

    POST https://api.smartsheet.com/2.0/sheets/2702602705784708/attachments
    
    Authorization: Bearer API_TOKEN_VALUE_HERE
    Content-Type: application/json
    
    {
        "name": "Bing",
        "url": "https://www.bing.com",
        "attachmentType": "LINK"
    }
    

    In cURL, this request would be specified as follows:

    curl https://api.smartsheet.com/2.0/sheets/{sheetId}/attachments \
    -H "Authorization: Bearer API_TOKEN_VALUE_HERE" \
    -H "Content-Type: application/json" \
    -X POST \
    -d "{\"name\": \"Bing\", \"url\": \"https://www.bing.com\", \"attachmentType\": \"LINK\"}"
    

    Upon issuing this request, I get the following response back:

    Attach URL response:

    {
        "message": "SUCCESS",
        "resultCode": 0,
        "result": {
            "id": 5022902287290244,
            "name": "Bing",
            "url": "https://www.bing.com",
            "attachmentType": "LINK",
            "parentType": "SHEET",
            "parentId": 2702602705784708,
            "createdAt": "2023-11-03T18:20:29Z",
            "createdBy": {
                "name": "Kim Brandl",
                "email": "*****@gmail.com"
            }
        },
        "version": 20
    }
    

    Bingo! -- the URL Attachment was successfully added to the sheet.

    Please note that the sample code above adds the URL attachment to the Sheet. To add a URL attachment to a Row, the request URL would need to include /rows/{rowId} as shown here:

    curl https://api.smartsheet.com/2.0/sheets/{sheetId}/rows/{rowId}/attachments \
    -H "Authorization: Bearer API_TOKEN_VALUE_HERE" \
    -H "Content-Type: application/json" \
    -X POST \
    -d "{\"name\": \"Bing\", \"url\": \"https://www.bing.com\", \"attachmentType\": \"LINK\"}"