A Step-by-Step Guide to bring AI to your App using Google App Script
Tags: ai, api, googleappscript, invoiceparser
Published: 2023-02-13 15:36:28

*In this tutorial, we'll show you how to integrate Eden AI's [Invoice parser API](http://www.edenai.co/feature/ocr-invoice?referral=integrate-invoice-parser-gas) into your mobile app 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](https://www.edenai.co/feature/object-detection?referral=integrate-invoice-parser-gas), [Explicit content detection](http://www.edenai.co/feature/explicit-content-detection?referral=integrate-invoice-parser-gas), [Text analysis](https://www.edenai.co/technologies/text?referral=integrate-invoice-parser-gas) and many more AI APIs we offer.*

‍

## **Before we start, what is Invoice Parsing?**

Just like [Receipt](https://www.edenai.co/post/top-10-ocr-receipt-parser-api?referral=integrate-invoice-parser-gas) and [Resume](https://www.edenai.co/post/top-10-ocr-resume-parser-api?referral=integrate-invoice-parser-gas) Parsing, Invoice Parsing is a tool powered by [OCR](https://www.edenai.co/post/analyze-easily-document-files-with-ai-optical-character-recognition-ocr?referral=integrate-invoice-parser-gas) to extract and digitalize meaningful data, [Computer Vision](https://www.edenai.co/post/best-computer-vision-apis?referral=integrate-invoice-parser-gas) to identify structure of the document, and [NLP](https://www.edenai.co/post/best-natural-language-processing-apis?referral=integrate-invoice-parser-gas) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jf6hiloanwl9cf6jaoc5.png)

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.

**[Try Eden AI for FREE](https://app.edenai.run/user/register?referral=integrate-invoice-parser-gas)**

## What is Google App Script?

Google App Script is a cloud-based platform that enables users to write code to automate tasks and enhance the functionality of Google services such as Google Sheets, Google Docs, and Google Forms. The platform is based on JavaScript, making it easy for developers with JavaScript experience. Google App Script integrates with other Google services and all code is run in the cloud, allowing for flexible and accessible script development.

## **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:

{% youtube fXArH2Ld9vk %}

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.](https://discord.com/invite/VYwTbMQc8u?referral=how-to-sentiment-analysis-java)

‍

*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.](https://docs.edenai.co/reference/ocr_invoice_parser_create?referral=integrate-invoice-parser-gas)

‍

*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](https://www.edenai.co/post/a-step-by-step-guide-to-integrating-edenai-invoice-parser-on-microsoft-powerapps?referral=integrate-invoice-parser-gas), [Retool](https://www.edenai.co/post/a-step-by-step-guide-to-implementing-ai-in-your-app-using-retool?referral=integrate-invoice-parser-gas), [n8n](https://www.edenai.co/post/a-step-by-step-guide-to-implementing-ai-in-your-app-with-n8n?referral=integrate-invoice-parser-gas), [Make](https://www.edenai.co/post/how-to-integrate-invoice-parsing-into-your-application-using-make?referral=integrate-invoice-parser-gas), and [IFTTT.](https://www.edenai.co/post/a-step-by-step-guide-to-implementing-ai-in-your-app-using-ifttt?referral=integrate-invoice-parser-gas)*

‍

**[Create your Account on Eden AI](https://app.edenai.run/user/register?referral=integrate-invoice-parser-gas)**