How to retrieve shareable URL using Google Drive REST API

I’m building an application that integrates with Google Drive and need help with generating shareable links programmatically. My goal is to create public sharing links for files by updating their permissions to allow access for anyone who has the link.

I can successfully modify the file permissions using the API, but I’m struggling to find where the actual shareable URL comes from. When I manually share a file through the web interface, it generates a link like:

https://docs.google.com/document/d/[file-id]/edit?usp=sharing

The problem is that this formatted URL doesn’t appear in the file metadata I get back from the API calls. I’ve checked the response from both the file retrieval and permission update endpoints, but neither includes this specific sharing URL format.

Can anyone explain how to construct or retrieve these shareable links programmatically through the Google Drive API?

Actually there’s another approach that might work better depending on your use case. The webContentLink field in the file metadata gives you a direct download link, but for proper sharing URLs you need to look at the response from the permissions.create() call itself. When you successfully add a permission with role ‘reader’ and type ‘anyone’, the API response includes a permissionId that you can use to construct the sharing URL pattern. I’ve found that different file types have slightly different URL structures though. For Google Docs it’s the format you mentioned, but for regular files uploaded to Drive the pattern is usually https://drive.google.com/file/d/[file-id]/view?usp=sharing. The file’s mimeType field helps determine which URL format to use. Also worth noting that there can be a brief delay before the sharing URL becomes fully active, so you might want to add a small buffer if you’re immediately redirecting users to these links.

just hit the files endpoint with ?fields=webViewLink,webContentLink after setting permissions and you’ll get both urls back. i always forget this too but the webViewLink is what you want for sharing - it automatically handles the proper format based on file type. no need to construct urls manually.

The shareable URL you’re looking for is actually constructed using the webViewLink field from the file metadata response. When you make a GET request to /drive/v3/files/{fileId} and include webViewLink in your fields parameter, the API returns the direct sharing URL. However, there’s a catch - the webViewLink only appears after you’ve set the appropriate permissions. Make sure you’re calling the permissions endpoint first to set the file as publicly accessible, then immediately fetch the file metadata again. The webViewLink field will contain the properly formatted sharing URL that matches what you see in the web interface. I ran into this exact issue last year and spent way too much time trying to manually construct the URLs. The key is the timing - permissions first, then retrieve the updated file metadata.