Code examples

Node.js

Run npm i form-data node-fetch to install required packages.

const fs = require("fs");
const path = require("path");
const fetch = require("node-fetch");
const FormData = require("form-data");
const internals = {
url: "https://api.taggun.io/api/receipt/v1/simple/file",
filePath: "./sample.jpeg",
taggunApiKey: "xxxx",
};
(async () => {
const filePath = internals.filePath;
try {
const postBody = createFormData(filePath);
const response = await fetch(internals.url, {
headers: {
accept: "application/json",
apikey: internals.taggunApiKey,
contentType: getContentType(filePath),
},
method: "POST",
body: postBody,
});
const result = await response.json();
console.log(result);
} catch (err) {
console.error(err);
}
})();
function createFormData(filePath) {
const filename = path.basename(filePath);
const fileStream = fs.createReadStream(filePath, { autoClose: true });
const formData = new FormData();
// Add any other POST properties that you require
// Go to https://api.taggun.io to see what other POST properties you require.
formData.append("file", fileStream, {
filename,
contentType: getContentType(filePath),
});
formData.append("refresh", "false");
return formData;
}
function getContentType(filePath) {
const fileExt = path.extname(filePath);
switch (fileExt.toLocaleLowerCase()) {
case ".png":
return "image/png";
case ".pdf":
return "application/pdf";
default:
return "image/jpg";
}
}

C# or .NET

See sample code for Taggun client in .NET C# here

CURL

curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' --header 'apikey: yourapikey' 'https://api.taggun.io/api/receipt/v1/verbose/file' -F 'file=@/file.jpg'

PHP

<?php
$filename = realpath( './uploads/20.jpg' );
$cfile = new CurlFile( $filename, 'image/jpeg', $filename );
$data = array( 'file' => $cfile );
$taggun_endpoint = 'https://api.taggun.io/api/receipt/v1/simple/file';
$ch = curl_init();
$options = array(
CURLOPT_URL => $taggun_endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_HEADER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array(
'apikey: YOUR API KEY',
'Accept: application/json',
'Content-Type: multipart/form-data',
),
CURLOPT_POSTFIELDS => $data,
);
curl_setopt_array( $ch, $options );
$result = curl_exec( $ch );
$header_info = curl_getinfo( $ch, CURLINFO_HEADER_OUT );
$header_size = curl_getinfo( $ch, CURLINFO_HEADER_SIZE );
$header = substr( $result, 0, $header_size );
$body = substr( $result, $header_size );
curl_close( $ch );
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>File Upload results</title>
</head>
<body>
<p>Raw Result: <?php echo $result; ?>
<p>Header Sent: <?php echo $header_info; ?></p>
<p>Header Received: <?php echo $header; ?></p>
<p>Body: <?php echo $body; ?></p>
</body>
</html>

python

import requests
url = 'https://api.taggun.io/api/receipt/v1/simple/file'
headers = {'apikey': 'yourapikey'}
files = {
'file': (
'1_Dinner.jpg', # set a filename for the file
open('1_Dinner.jpg', 'rb'), # the actual file
'image/jpg'), # content-type for the file
# other optional parameters for Taggun API (eg: incognito, refresh, ipAddress, language)
'incognito': (
None, #set filename to none for optional parameters
'false') #value for the parameters
}
response = requests.post(url, files=files, headers=headers)
print(response.content)