A Step-by-Step Guide to bring AI to your App using Google App Script
Tutorial

A Step-by-Step Guide to bring AI to your App using Google App Script

In this tutorial, we'll show you how to integrate Eden AI's Invoice parser API into your software using Google App Script. With this integration, you'll be able to automate tasks and enhance the functionality of Google services using the power of AI. The same process applies if you want to include other features like : Image tagging, Explicit content detection, Text analysis and many more AI APIs we offer.

Build AI on Google App Script with Eden AI

Eden AI was created for no-coders having difficulties with choosing the right AI provider and/or integrating several APIs. Managing multiple accounts for each app can be a tough job, but with Eden AI, you can connect and manage all your APIs on a single account. Since some AI providers can be complex to use, we wanted to simplify the “no code” part and make AI available to as many people as possible. 

Eden AI allows you to solve multiple AI tasks on Google App Script:

Let's practice with Invoice parsing!

Just like Receipt and Resume Parsing, Invoice Parsing is a tool powered by OCR to extract and digitalize meaningful data, Computer Vision to identify structure of the document, and NLP techniques to pin down the fields. Invoice parser technology extracts key information from an invoice (.pdf, .png or .jpg format) such as the invoice ID, total amount due, invoice date, customer name, etc.

Invoice Parsing results on Eden AI

Invoice parser APIs help ensure data accuracy by avoiding errors that can occur during manual data extraction. Then, this information can be used for a variety of purposes and makes it easier to generate reports or export data to other applications and is often used in conjunction with other business management applications.

How to add invoice parsing to your app with Google App Script?

1. Initialize the invoice_parser function 

The first step is to initialize the invoice_parser function that will handle the API call to Eden AI. This function will take 4 arguments: api_key, providers, file, and language.

The api_key is the API key you obtained from Eden AI platform, providers is the name of the provider you want to use to extract the invoice data , file is the invoice file you want to extract data from, and language is the language of the invoice. 


function invoice_parser(api_key, providers, file, language) { 
	//... 
} 

2. Create the payload with the FormData 

Next, use the FormData library to create the payload for the API request:

The payload contains information about the invoice file and the desired response format. You’ll need to append the providers, response_as_dict, file, and language to the payload. The response_as_dict parameter is set to false so that the response is returned as a JSON string rather than a dictionary. 


function invoice_parser(api_key, providers, file, language) {
	const formData = FormData.newFormData();  
	formData.append('providers', providers);  
	formData.append('response_as_dict', false);  
	formData.append('file', file);
	formData.append('language', language);  
	const payload = formData.getPayload(); 
} 

3. Create the header 

Next, create the header for the API request. The header includes the Authorization and Content-Type parameters.

The Authorization parameter is set to the Bearer Token obtained from Eden AI's platform, and the Content-Type parameter is set to multipart/form-data with the boundary value set to the boundary string obtained from the form data. 


function invoice_parser(api_key, providers, file, language)
{
	//...  
	const headers = {  
		"Authorization": `Bearer ${api_key}`,  
		"Content-Type": `multipart/form-data; boundary=${formData.getBoundary()}`
  } 
}

4. Make the API call with UrlFetchApp 

Now, use the UrlFetchApp library available in the Google App Script standard library to make the API call to Eden AI. You’ll need to set the method to POST, the headers to the header we just created, the payload to the payload we created in step 2, and muteHttpExceptions to true. 


function invoice_parser(api_key, providers, file, language) {
	//...  
	const response = UrlFetchApp.fetch(  
		`https://api.edenai.run/v2/ocr/invoice_parser`,  
		{  
			"method": "POST",  
			"headers": headers,  
			"payload": payload,  
			"muteHttpExceptions": true,  
		}
  );
	return response 
} 

5. Load your file from Google Drive 


const file = DriverApp.getFileById("");

6. Parse the response data with JSON 


const api_key = "YOUR EDEN AI API KEY" 
const providers = "base64" 
const language = 'en' 
const response = invoice_parser(api_key, providers, file, language)
const response_json = JSON.parse(response) 

7. Access the extracted data 

Finally, the extracted data is accessed using the response_json object. In this case, the Logger.log method is used to print the extracted data in the log. However, you can access and use the extracted data in any way you wish, for instance, to create a Google Sheet with the data. 


Logger.log(response_json['base64']['extracted_data']) 

8. Recap

You’re done! 🥳 


function invoice_parser(api_key, providers, file, language) {
	// Create the payload with the FormData library available at https://github.com/edenai/FormData  
	const formData = FormData.newFormData();  
	formData.append('providers', providers);  
	formData.append('response_as_dict', false);  
	formData.append('file', file);  
	formData.append('language', language);  
	const payload = formData.getPayload();  
	
  // Create the header  
	const headers = {  
		"Authorization": `Bearer ${api_key}`,  
		"Content-Type": `multipart/form-data; boundary=${formData.getBoundary()}`  
	}
  
	// Make the api call with UrlFetchApp in standard library  
	const response = UrlFetchApp.fetch(  
		`https://api.edenai.run/v2/ocr/invoice_parser`, 
		{
		"method": "POST",  
		"headers": headers,  
		"payload": payload,
		"muteHttpExceptions": true,  
		}  
	)  
	return response 
} 

function main() {
	const file = DriveApp.getFileById("");  
	// You can find your api_key at https://app.edenai.run/  
	const api_key = ""  
	const providers = "base64"  
	const language = 'en'  
	const response = invoice_parser(api_key, providers, file, language)  
	const response_json = JSON.parse(response)  
	// Now you  can use your result as you want, for example to  create a sheet with all data  
	// Here we just print all of field found by the providers  
	Logger.log(response_json['base64']['extracted_data']) } 

If you have questions regarding this tutorial, the Eden AI community can help you out on Discord.

Note: The above code is just an example of how to use the invoice parser API from Eden AI. 

The specific fields and data that the API returns may vary based on the type of document being parsed and the provider being used. 

For more information, check out the full documentation for the invoice parser API.

If you're interesting in more low-code tools, have a look at our step-by-step tutorials on how to bring AI to your application with Power Apps, Retool, Make, IFTTT, n8n, Bubble, Dataiku, and Zapier.

Related Posts

Try Eden AI for free.

You can directly start building now. If you have any questions, feel free to schedule a call with us!

Get startedContact sales