Search code examples
curlimgur

How can I upload an image to my account in imgur.com with curl?


Just that.

curl: a command line tool to operate with URLs

imgur.com : a service that allows to upload and share images


Solution

  • Straigth to the answer.

    # curl -X POST -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -F "image=@PATH_TO_YOUR_IMAGE_FILE" https://api.imgur.com/3/upload
    

    example

    # curl -X POST -H "Authorization: Bearer 9c9d....9b41f" -F "image=@/tmp/pet.png" https://api.imgur.com/3/upload
    

    You will get an JSON response that looks like this

    {"status":200,"success":true,"data":{"id":"m1Jv","deletehash":"zMI6VN","account_id":2583,"account_url":"ruxr","ad_type":null,"ad_url":null,"title":null,"description":null,"name":"","type":"image/png","width":169,"height":120,"size":3371,"views":0,"section":null,"vote":null,"bandwidth":0,"animated":false,"favorite":false,"in_gallery":false,"in_most_viral":false,"has_sound":false,"is_ad":false,"nsfw":null,"link":"https://i.imgur.com/m1v.png","tags":[],"datetime":16756,"mp4":"","hls":""}}
    

    and in

    "link":"https://i.imgur.com/m1v.png"
    

    you get your answer (this link won't work, just an example)

    You can get that link out easily with jq

    # echo "$response_json" | jq  --raw-output '.data.link'
    

    Some background.

    There are lots of people asking this question around.

    I checked and rechecked the API documentation and couldn't find the answer.

    I was using imgur_uploader.py for a while but now it started failing due to rate limit (or maybe my token expired), which couldn't be the case because I was uploading just one image every x minutes. Other people were facing similar issues.

    File "/home/me/.local/lib/python3.5/site-packages/imgurpython/client.py", line 596, in upload_from_path
        return self.make_request('POST', 'upload', data, anon)
    File "/home/me/.local/lib/python3.5/site-packages/imgurpython/client.py", line 153, in make_request
        raise ImgurClientRateLimitError()
    imgurpython.helpers.error.ImgurClientRateLimitError: Rate-limit exceeded!
    

    After some digging I got the python script working again, but images where not uploaded inside my account. So I started searching again.

    Here [A] I found the answer

    To get your Access_token use this [B] and then inspect the resulting url

    [A] https://planspace.org/2013/01/13/upload-images-to-your-imgur-account/

    [B] https://stackoverflow.com/a/61343188/4752223