Skip to main content

How to programmatically retrieve public id from URL?



  • Michal Kuperman

    Hi Peters,

    We don't have an API to retrieve the public_id from the URL. However, when you upload an image, the upload API call returns a response that includes the public_id.
    You can save the public_id and then get the URL and all the information by using the Admin API. Read here for more info:


  • Peters Oluwafemi

    Okay does the public id include the folder names . 

  • Aleksandar Kostadinov

    Hi Peters,

    Yes, the public_id contains all folders and the last part of the public_id is the filename.

  • Every Org

    Follow up to this question: given that we didn't store the images at the time of upload, is there a way to reliably get an image ID from a url when it may contain transforms? The above answers make it sound like the answer is "no" and we need to store the IDs alongside the raw URLs, but would be nice to confirm if there really is no way.

    For example, when we wanted to use a transform to combine two cloudinary urls into one with the other superimposed on it with the `l_{cloudinaryImageId}` tag in the transform, we needed to get an image IDs from two existing Cloudinary URLs, but the presence of folders made it unwieldy to do this in an way that feels safe. If there is a way, it would save us engineering time/db migration risk to know. Thanks!

  • Shirly Manor


    As you said, our best practice will be to have the public_id handy. and use it whenever you need.

    Currently, the way to find the public_id is only by string manipulation. That is not our recommended way. because there are some not standard ways to use our urls.

    However, if you are using Cloudinary without our SEO features. you can search for the second indexof `/` after upload. and until the last index of the `.`.

    A better way will be to get the public id with our [search API]( and use them.

    Hope that helps,

  • dima

    I went with a regular expression as follows:

    $url = '';

    preg_match("/upload\/(?:v\d+\/)?([^\.]+)/", $url, $matches);

    // $matches[1] contains "some-video"

    // works also for
    "" === "some-image"
    "" === "some-thing"
    "" === "some-thing"

    I did not implement transformations in the URLs, as we did not need it in our current project.

  • Aleksandar Kostadinov

    Hi Dima,

    That approach would work. However, if you can, I would recommend performing a one-off migration of the data you store in your database to store the individual parts of the asset's identifier (e.g. resource_type, type and public_id) rather than the whole URL. This way, it'll be a lot easier for you to use any of the API methods or local SDK methods to build URLs without needing RegExp.

    In addition, even if you don't need any custom transformations to change the visual aspects of the image, it's always best to include transformations that optimize the delivery of images rather than requesting originals which are most often large in size. For example, such as f_auto and q_auto transformations. Below I'm including two links to relevant support article/documentation with regards to image optimizations that provide more details -

  • David Ortega

    hey guys! in our case we are saving media files in Cloudinary through Strapi CMS, and when we get data from there we only receive the Cloudinary's URL, I hope you can find a way to get the public_Id from the url soon.

  • Aleksandar Kostadinov

    Hi David,

    I'm not familiar with Strapi myself, but I was wondering, how much control do you have (if any) in terms of the data you get back? When using the Cloudinary Media Library Widget to export/insert images from Cloudinary, we return a response similar to the of the API when you've uploaded a new image, so that includes the public_id also.

    In addition, could you share a few of the URLs you get back from the system - are they with the full version number? If so, that could allow you to use a simpler regular expression to extract the public_id. 


Post is closed for comments.