Q:

Create text file and upload it to document library using REST API with JavaScript

We need to create a text file and upload it to a document library using REST API. The contents of text file will be created dynamically  by reading information from multiple lists. The operation needs to be performed using REST API and file is not to be saved in user's machine. Is this possible?

A:

Yes, its possible.
Try and modify the below code. Fill the content with data from your lists:
var content = "Hello, this text is inside the file created with REST API";
var digest = $("#__REQUESTDIGEST").val();
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('/sites/test/Shared Documents')/Files/add(overwrite=true, url='test.txt')";
jQuery.ajax({
    url: url,
    type: "POST",
    data: content,
    headers: {        
        "accept": "application/json;odata=verbose", 
        "X-RequestDigest": digest       
    },
    success: function(data){
        console.log(data);
    },
    error: function(data){
        console.log(data);
    }
});

