There is no clear documentation provided by Microsoft for REST API for Sharing Document.
There is only one link which gives little bit details about what is the request model for passing details in API which I found. Rest of the detailed I covered it from Browser developer tools -> Network tab.
https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-csomspt/50cd1911-fdc5-4e87-9a1d-29cf39f13ebd
REST API: _api/web/lists/getbytitle('{Document Library Title}')/getitembyid({Item ID})/sharelink
Request Type: POST
You need to add authentication token and request data digest into request. Below are helper links for it:
Call API: https://jeremythake.com/using-the-sharepoint-csom-and-rest-api-with-office-365-api-via-azure-ad-fe59a6d1a26
Get Request Digest using API: https://www.ozkary.com/2018/09/sharepoint-rest-api-post-with-token.html
Post Data:
I am sharing C# model which you can use to convert it JSON at the end
public class ShareLinkRequestModel
{
public Request request { get; set; }
}
public class Request
{
public bool createLink { get; set; }
public Settings settings { get; set; }
public string peoplePickerInput { get; set; }
}
public class Settings
{
public SharingLinkKind linkKind { get; set; }
public object expiration { get; set; }
public Microsoft.SharePoint.Client.Sharing.Role role { get; set; }
public bool restrictShareMembership { get; set; }
public bool updatePassword { get; set; }
public string password { get; set; }
public Microsoft.SharePoint.Client.Sharing.SharingScope scope { get; set; }
}
public class PeoplePickerInputModel
{
public string Key { get; set; }
public bool IsResolved { get; set; }
}
Above API will work for both type of users i.e. Internal and External users.
Sample C# Code to do so for better understanding: https://github.com/kavaghela/sharedocumentspapi
Important: Make sure that External Sharing is enabled in your site in order to share content with external users. If you will share with external users which is not added in your tenant then while that external user will access shared document, Microsoft will do OTP verification (sent in email) to identify identity
How to enable External Sharing: https://docs.microsoft.com/en-us/sharepoint/change-external-sharing-site