Create and Upload a File

How do I create and upload a new file?

There are two ways to create and upload a new file: either by uploading the file directly, or by providing a URL where your file is accessible. This does not necessarily have to be a fully public URL in many cases. For example, if you upload a file to Amazon S3, you can use a presigned URL that expires in 5 minutes, then share that with Docalysis, without making the file itself public.

The file will be stored and processed by Docalysis. Processing happens asynchronously, which is why the processed_state in the API response to a create request is 'unprocessed'.

Some other optional parameters include:

'publicly_available', if set to true, will make the file publicly available. This is false by default.

'path' if present will put the file in the directory at that path from your root. Path names are case sensitive. 'parent_directory_id' if present will put the file in the directory with that id. Both 'path' and 'parent_directory_id' cannot be used at the same time.

Example request body:

  
{
  "name": "my_file.pdf",
  "url": "https://mybucket.s3.amazonaws.com/somefilehere.pdf"
}
  
  

Example response:

  
{
	"success": true,
	"file": {
		"id": "h444",
		"created_at": 1686887578000,
		"file_size": 184292,
		"file_type": "pdf",
		"name": "my_file.pdf",
		"page_count": 9,
		"processed_state": "unprocessed"
	}
}
  
  

Ruby code to create and upload a local file:

    
require 'net/http'
require 'uri'

def upload_file(endpoint, file_path, headers = {}, form_data = {})
  uri = URI(endpoint)

  request = Net::HTTP::Post.new(uri)

  headers.each do |key, value|
    request[key] = value
  end

  form_data_arr = [['file', File.open(file_path)]]
  form_data.each do |key, value|
    form_data_arr << [key, value]
  end

  request.set_form form_data_arr, 'multipart/form-data'

  Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end
end

endpoint = 'https://api1.docalysis.com/api/v1/files/create'
file_path = '/Users/you/path/to/your/file.pdf'
headers = { 'Authorization' => 'Bearer your_api_key_here' }
form_data = {
  'name' => 'desired_file_name_here.pdf'
}

response = upload_file(endpoint, file_path, headers, form_data)
puts response.body
    
  

Ruby code to create and upload a file from a URL:

    
require 'net/http'
require 'net/https'
require 'uri'
require 'json'

name = 'my_file.pdf' # Replace with your desired file name
url = 'https://mybucket.s3.amazonaws.com/somefilehere.pdf' # Replace with your file URL

uri = URI.parse("https://api1.docalysis.com/api/v1/files/create")

request = Net::HTTP::Post.new(uri)
request["Authorization"] = "Bearer your_api_key_here"
request["Content-Type"] = "application/json"
request.body = JSON.dump({
  'name' => name,
  'url' => url
})

req_options = {
  use_ssl: true,
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end

puts response.body