Here we will discuss how we can create a document using Rest API in SharePoint Online. The sane rest api code will also work in SharePoint 2016 and SharePoint 2013.

Here in this particular example let us take a textbox, a multiple textbox as well as a button. Here user will give a name for the file and user can put the file content in the multiline textbox. Then user can submit in the button which will create a text file.

Here we will write both the html code as well as in the JavaScript object model (jsom) inside a script editor web part which will be inside a web part page. The html code will looks like below:

HTML Code:

<div id="CreateFile">
<strong>Enter Document Name:</strong>
<br />
<input type="text" id="txtDocumentTitle" />
<strong>Enter Document Content:</strong>
<br />
<textarea cols="20" id="txtDocumentContent"></textarea>
<br />
<input type="button" id="btnSubmit" value="Submit" />
<div id="divResults"></div>

Rest API Code:

$(function () {
function bindButtonClick() {
$("#btnSubmit").on("click", function () {
function createDocument() {
var docTitle = $("#txtDocumentTitle").val() + ".txt";
var docContent = $("#txtDocumentContent").val();
var fullUrl =  _spPageContextInfo.webAbsoluteUrl+ "/_api/web/GetFolderByServerRelativeUrl('Shared Documents')/Files/add(url='" + docTitle +"',overwrite=true)";
url: fullUrl,
type: "POST",
data: docContent,
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
success: onQuerySucceeded,
error: onQueryFailed
function onQuerySucceeded() {
$("#divResults").html("Document successfully created!");
function onQueryFailed() {

Once we will Save the page you can see a page like below where user can give a title for the file, then user can put content in the content textbox and then user can click on the submit button which will create a file inside the document library like below:

Now you can check in the document library where you can see the file got created in the document library.


You can also read few useful articles below:


Here we have disucssed how we can create a document using Rest API in SharePoint Online.