This endpoint enables users to upload files to a specified extraction. If a batchId is included in the request, the files will be added to that specific existing batch on the platform. It is important to ensure that the batchId already exists; otherwise, the upload will not be successful. If no batchId is provided in the request, a new batch will automatically be created for the files.
Files must be uploaded using the multipart/form-data content type, which is suitable for uploading binary files (like documents and images).
Postman Collection
For a complete and interactive set of API requests, please refer to our Postman Integrationcollection.
For a seamless extraction process, please ensure your documents are in one of our supported formats. Check our Supported File Types page for a list of all formats we currently accept and additional details to prepare your files accordingly.
Note for PHP Users: Currently, the /uploadFiles endpoint supports uploading only one file per request. Please ensure you submit individual requests for each file you need to upload.
constfs=require('fs');constaxios=require('axios');constFormData=require('form-data');/** * Uploads files to the Extracta API using Axios for making HTTP requests. * * @param{string} token - The authorization token to access the API. * @param{string} extractionId - The ID of the extraction process to which these files belong. * @param{Array.<string>} files - Paths to the files to be uploaded. * @param{string} [batchId=null] - Optional batch ID if the files belong to a specific batch. * @returns{Promise<Object>} The promise that resolves to the API response. */asyncfunctionuploadFiles(token, extractionId, files, batchId =null) {consturl="https://api.extracta.ai/api/v1/uploadFiles";let formData =newFormData();formData.append('extractionId', extractionId);if (batchId) {formData.append('batchId', batchId); }// Append files to formDatafiles.forEach(file => {formData.append('files',fs.createReadStream(file)); });try {constresponse=awaitaxios.post(url, formData, { headers: {...formData.getHeaders(),'Authorization':`Bearer ${token}` },// Axios automatically sets the Content-Type to multipart/form-data with the boundary. });// Handling responsereturnresponse.data; // Directly return the parsed JSON response } catch (error) {// Handling errorsthrowerror.response ?error.response.data :newError('An unknown error occurred'); }}asyncfunctionmain() {consttoken='apiKey';constextractionId='extractionId';constfiles= ['test_1.png','test_2.png'];try {constresponse=awaituploadFiles(token, extractionId, files);console.log(response); } catch (error) {console.error("Failed to upload files:", error); }}main();
import requestsdefupload_files(token,extraction_id,files,batch_id=None): url ="https://api.extracta.ai/api/v1/uploadFiles" headers ={"Authorization":f"Bearer {token}"}# Prepare the files for uploading file_streams = [ ("files", ( file,open(file, "rb"), mimetypes.guess_type(file)[0] or"application/octet-stream", ), )for file in files ] payload ={"extractionId": extraction_id}if batch_id isnotNone: payload["batchId"]= batch_idtry: response = requests.post(url, files=file_streams, data=payload, headers=headers) response.raise_for_status()# This will raise an error for HTTP codes 400 or 500return response.json()# Returns the JSON response if no errorexcept requests.HTTPError as e:# Print server-side error messageif response.status_code >=400: error_message = response.json()print(f"Server returned an error: {error_message}")else:print(f"HTTP error occurred: {e}")except requests.RequestException as e:# Handle other requests exceptionsprint(f"Failed to upload files: {e}")exceptExceptionas e:# Handle other possible exceptionsprint(f"An unexpected error occurred: {e}")returnNone# Example usageif__name__=="__main__": token ='apiKey' extraction_id ='extractionId' files = ['test_1.png','test_2.png']try: response =upload_files(token, extraction_id, files)print(response)exceptExceptionas e:print(f"Failed to upload files: {e}")
<?php/** * Uploads files to the Extracta API using cURL for making HTTP requests. * * @paramstring $token The authorization token to access the API. * @paramstring $extractionId The ID of the extraction process to which these files belong. * @paramstring $filePath Path to the file to be uploaded. * @paramstring|null $batchId Optional batch ID if the file belongs to a specific batch. * @returnmixed The response from the API or an error message. */functionuploadFiles($token, $extractionId, $filePath, $batchId =null) { $url ='https://api.extracta.ai/api/v1/uploadFiles';// Initialize cURL session $ch =curl_init($url);// Prepare the payload $payload = ['extractionId'=> $extractionId,'files'=>newCURLFile($filePath,'image/jpeg',basename($filePath)) ];if ($batchId !==null) { $payload['batchId'] = $batchId; }// Set cURL optionscurl_setopt($ch, CURLOPT_POST,1);curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer '. $token,'Content-Type: multipart/form-data' ]);curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);try {// Execute cURL session $response =curl_exec($ch);// Check for cURL errorsif (curl_errno($ch)) {thrownewException('Curl error: '.curl_error($ch)); }// Here, you could additionally parse the $response if it's in JSON format or if needed,// For simplicity, just returning the raw response for nowreturn $response; } catch (Exception $e) {// Handle exceptions or errors herereturn'Error: '. $e->getMessage(); } finally {// Always close the cURL sessioncurl_close($ch); }}// Example usage$token ='apiKey';$extractionId ='extractiodId';$filePath ='./test_1.png';$batchId =null;try { $response =uploadFiles($token, $extractionId, $filePath, $batchId);echo $response;} catch (Exception $e) {echo"Failed to upload file: ". $e->getMessage();}?>