Welcome to the new Box Support website. Check out all the details here on what’s changed.

Curl + shared link

Answered
New post

Comments

11 comments

  • cbetta

    Hi  , a web link is not a shared link.

     

    To get a shared link for a file, update the file to enable the shared link for it.

    curl -X PUT https://api.box.com/2.0/files/12345 \
         -H 'Authorization: Bearer " '
         -H 'Content-Type: application/json" '
         -d '{
           "shared_link": {
             "access": "open"
           }
         }'

     

    https://developer.box.com/reference/put-files-id/#param-shared_link

     

    You can use our SDK to do this as well.

     

    https://developer.box.com/guides/shared-links/create/#Create-Shared-Link-for-File

    0
    Comment actions Permalink
  • Amelie

    If I understand it good, in the example of , the 'acces token' must be replaced by the API I get from my box account, and the '12345' is the ID of my file. How do I get the ID of the file (with a program). And, if I enabled schared link, how does I get it?

     

    Because, in the end, I want to write script that can send a link to share the file on my box account. Composing a mail is not a problem, but getting the link to share..

     

     

     

    0
    Comment actions Permalink
  • cbetta

    You can get the file ID a few different ways. Either, you can go to box.com and find the file and open the preview. You can then see the ID of the file in the URL bar:

     

    Screenshot 2020-04-22 at 13.43.20.png

    In this case the part after "file/" is the file ID.

     

    Another way is to find the file ID by using the API. You can traverse the files and folders in your Box account with our APIs.

    https://developer.box.com/reference/get-folders-id-items/

    https://developer.box.com/guides/folders/bulk/build-folder-tree/

    ---

    By the way, depending on your programming skills you might wan to use our Box CLI instead.

     

    https://github.com/box/boxcli

    0
    Comment actions Permalink
  • kliger

    Hi  , I tried using your advice and set shared_link attribute on an existing file:

     

     

    curl -X PUT https://api.box.com/2.0/files/*** \
         -H 'Authorization: Bearer ' \
         -H 'Content-Type: application/pdf"' \
         -d '{
           "shared_link": {
             "access": "open"
           }
         }'   

     

     

    The request was successful. But then when I check the file stats, for some reason the "shared_link" is set to null, and I cannot create a shared link. 

     

    Also, do you know if there is a way to get a shared link for a file using curl? It looks like in the docs there are only four SDK examples.

     

    0
    Comment actions Permalink
  • cbetta

    Have you tried to request the shared link field on the file?

    curl https://api.box.com/2.0/files/1234?fields=shared_link

     

    Does this return a file object with a shared link set to null?

    0
    Comment actions Permalink
  • kliger

     That command doesn't give me any output at all (no errors either)

     

    I tried running this command instead:

     

    curl -X GET https://api.box.com/2.0/files/67***phone number removed for privacy***?fields=shared -H 'Authorization: Bearer '

     

    And the response is:

    {"type":"file","id":"67***phone number removed for privacy***","etag":"0","shared_link":null}

    0
    Comment actions Permalink
  • cbetta

    Interesting. What kind of Enterprise are you using? Are you on a free personal account? Could you try setting the shared link in the web app and then requesting it via the API?

     

    I just tested and this definitely works:

     

    Set the shared link

    curl --location --request PUT 'https://api.box.com/2.0/files/123456789' \
    --header 'Content-Type: application/json' \
    --header 'Authorization: Bearer [TOKEN]' \
    --data-raw '{
      "shared_link": {
        "access": "open"
      }
    }'

     

    Get the shared link

    curl --location --request GET 'https://api.box.com/2.0/files/123456789?fields=shared_link' --header 'Authorization: Bearer [TOKEN]' 

     

    0
    Comment actions Permalink
  • kliger

     We're on Enterprise account.

    Unfortunately this did not help. That curl command runs successfully but then I get as a response to the second one.

     

    {"type":"file","id":"***","etag":"1","shared_link":null}

     

    I was able to set he shared link in the web app but then API still reports it as null.

    I think I will try to open a ticket with support to investigate further.

    0
    Comment actions Permalink
  • cbetta

     can you confirm for me that you can create shared links via the web app?

     

    We do have some admin settings to prevent users from creating shared links.

     

    Screen Shot 2020-06-10 at 10.44.50 AM.png

     

    We also have settings to restrict the visibility of shared links.

     

    Screen Shot 2020-06-10 at 10.46.11 AM.png

     

    It might be that if you try the following your API call will work.

     

    curl --location --request PUT 'https://api.box.com/2.0/files/123456789' \
    --header 'Content-Type: application/json' \
    --header 'Authorization: Bearer [TOKEN]' \
    --data-raw '{
      "shared_link": {
        "access": "collaborators"
      }
    }'

    This would create a shared link that is only accessible to those who already have access to the file. You can see all the possible values here. https://developer.box.com/reference/put-files-id/#param-shared_link-access

    0
    Comment actions Permalink
  • kliger

    We found out that if we create a custom app with Oauth2+JWT authentication, it works.

    Before we were using App Tokens, and that's when the API "fails silently" without creating a shared link.

    0
    Comment actions Permalink
  • cbetta

    Ahh yes, we generally advice against using App Tokens, they are generally only intended to be used in the context of Box View.

    0
    Comment actions Permalink

Please sign in to leave a comment.