Skip to main content
All CollectionsSetup Data FlowSetup Data Flow
How to Connect to Gaviti using an HTTP Endpoint (API)
How to Connect to Gaviti using an HTTP Endpoint (API)
Updated over a week ago

Sending data using HTTP is available for
1. Data Files
2. Attachment Files (usually PDF files).

In case you need to whitelist Gaviti's IP addresses, refer to this article.

Adapter URL and Adapter Token

To obtain the adapter URL and token, navigate to Settings > Integrations in Gaviti Core application. Locate the relevant adapter and click on the ⚙️ icon.
You will find the token and the name of the adapter there.

The name is used to build the adapter URL, which will be either:

  • For EU servers: https://api.gaviti.com/v2/adapter/run/[ADAPTER-NAME]
    or

  • For US servers: https://us.api.gaviti.com/v2/adapter/run/[ADAPTER-NAME]

Data Files

You can send data to Gaviti using an HTTP endpoint by specifying the content of your data and the URL and token of the data adapter.

Supported Content Types

XLSX

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

XLS

application/vnd.ms-excel

CSV

text/csv

TXT

text/plain

JSON

application/json

How to send the data

You can automate the process of sending data to Gaviti using common HTTP clients, programmable code, or PowerShell scripts.

Query Parameters:

  • token (mandatory): the secret token of the adapter

  • async (optional): if set to true, run an asynchronous call and the processed results won't be sent in the response

Request Body:

  • The content of the request is coming from a file

  • The body should be defined in a content type (not specified in the question)

Optional:

  • You can gzip the content. If you choose so, the content encoding should be set to gzip.

Examples

PowerShell:

$headers = @{
"Content-Type" = "text/csv"
}
$body = [System.IO.File]::ReadAllBytes("path/to/file.csv")
$token = "your-secret-token"
$adapter_name = "adapter-name"
$async = "false"

Invoke-RestMethod -Method Post -Uri "https://api.gaviti.com/v2/adapter/run/${adapter_name}?token=${token}&async=${async}" -Headers $headers -Body $body

Bash curl:

body=$(gzip -c path/to/file.csv)
token="your-secret-token"
adapter_name="adapter-name"
async="false"

curl -X POST \
"https://api.gaviti.com/v2/adapter/run/$adapter_name?token=$token&async=$async" \
-H "Content-Encoding: gzip" \
-H "Content-Type: text/csv" \
--data-raw "$body"

Attachment Files

In order to attach a PDF file to an invoice in the Gaviti application, you need to convert the PDF file into a base64 string. To do this, follow these steps:

  1. Convert the PDF file into a byte array.

  2. Convert the byte array into a base64 string.

  3. Add to your API call the HTTP header: "Content-Type" = "application/json"

Note that each invoice must be sent one at a time.

The PDF filename must be either the invoice id, the invoice display id, or an invoice custom field. If you choose to use an invoice custom field as the filename, it must be provided in the invoice data file.

Let's take a look on three different options separately.

Option 1: Invoice id as a filename for PDF file.

For example, if the invoice id is INV-1234
The PDF filename should be INV-1234.pdf
The JSON body request (payload) for this case would be as follows:

{
"invoiceId": "INV-1234",
"fileName": "INV-1234.pdf", // pay attention we need extension here
"token": "[ATTACHMENT-ADAPTER-TOKEN]",
"data": "[BASE64-ENCODED-RAW-PDF-FILE-DATA-HERE]",
"allowNotExistsInvoices": false // default is false
}

Option 2: Invoice display id as a filename for PDF file.

For example, if the invoice display id is INV-1234-DID
The PDF filename should be INV-1234-DID.pdf
The JSON body request (payload) for this case would be as follows:

{
"invoiceId": "INV-1234-DID",
"fileName": "INV-1234-DID.pdf",
"token": "[ATTACHMENT-ADAPTER-TOKEN]",
"data": "[BASE64-ENCODED-RAW-PDF-FILE-DATA-HERE]",
"mode": "displayId",
"idMultiplicationAllowed": true,,
"allowNotExistsInvoices": false // default is false
}

Option 3: The filename provided in invoice custom field.

For example, if the invoice id is INV-1234, the invoice display id is INV-1234-DID, and invoice has a custom field pdfFileName with value pdf-invoice-202212271234
The PDF filename should be pdf-invoice-202212271234.pdf.
The JSON body request (payload) for this case would be as follows:

{
"invoiceId": "pdf-invoice-202212271234",
"fileName": "pdf-invoice-202212271234.pdf",
"token": "[ATTACHMENT-ADAPTER-TOKEN]",
"data": "[BASE64-ENCODED-RAW-PDF-FILE-DATA-HERE]",
"mode": "pdfFileName",
"idMultiplicationAllowed": false,,
"allowNotExistsInvoices": false // default is false
}


Additional options:

Some of the options has an optional field called idMultiplicationAllowed. The default value set to false.
If this field is set to true the system will upload the same PDF file to all invoices with the same invoice display id or the same value from invoice custom field.

If this field is set to false the system will upload the PDF file to one invoice id only.

Additional optional field called allowNotExistsInvoices allow you to upload the PDFs for still none exists invoices in Gaviti Core application. By default set to false.

Examples

PowerShell (one file):

param(
[string]$fileName
)

$filePath = $fileName
$invoiceId = [System.IO.Path]::GetFileNameWithoutExtension($fileName)
$token = "[TOKEN]"
$adapterName = "[ADAPTER NAME]"
$apiUrl = "https://api.gaviti.com/v2/adapter/run/$adapterName"
$headers = @{
"Content-Type" = "application/json"
}

# Read the PDF file as raw binary data
$pdfData = [System.IO.File]::ReadAllBytes($filePath)

# Convert the binary data to Base64-encoded string
$base64Data = [System.Convert]::ToBase64String($pdfData)

# Create the JSON object
$jsonObject = @{
invoiceId = $invoiceId
fileName = $fileName
token = $token
data = $base64Data
}

# Convert the object to JSON format
$jsonString = ConvertTo-Json $jsonObject

# Send the JSON data to the API endpoint
Invoke-RestMethod -Method Post -Uri $apiUrl -Headers $headers -Body $jsonString

PowerShell (Multiple files):

$folderPath = "C:\example\"
Get-ChildItem $folderPath | Where-Object { $_.PSIsContainer -eq $false } | ForEach-Object {
$invoiceId = [System.IO.Path]::GetFileNameWithoutExtension($_.Name)
$token = "[TOKEN]"
$adapterName = "[ADAPTER NAME]"
$apiUrl = "https://us.api.gaviti.com/v2/adapter/run/$adapterName"
$headers = @{
"Content-Type" = "application/json"
}

# Read the PDF file as raw binary data
$pdfData = [System.IO.File]::ReadAllBytes($folderPath + $_.Name)

# Convert the binary data to Base64-encoded string
$base64Data = [System.Convert]::ToBase64String($pdfData)

# Create the JSON object
$jsonObject = @{
invoiceId = $invoiceId
fileName = $_.Name
token = $token
data = $base64Data
}

# Convert the object to JSON format
$jsonString = ConvertTo-Json $jsonObject

# Send the JSON data to the API endpoint
Invoke-RestMethod -Method Post -Uri $apiUrl -Headers $headers -Body $jsonString

}

Here's how to use the script:

  1. Replace the [TOKEN] placeholder in the script with your actual authentication token.

  2. Replace the [ADAPTER NAME] placeholder in the script with the name of the adapter you want to run.

  3. Save the script with a .ps1 extension, like pdf-to-gaviti.ps1.

  4. Open PowerShell and navigate to the directory where the script is located.

  5. Run the script by typing .\pdf-to-gaviti.ps1 "C:\path\to\pdf\file.pdf", replacing C:\path\to\pdf\file.pdf with the actual path to the PDF file you want to transform and send to the API.

  6. Press Enter to execute the command.

That's it! The script will read the specified PDF file, extract the invoice ID from the file name, transform the file data to the specified JSON format, and send the JSON data to the API endpoint using a POST request with the specified headers and authentication token. The response from the API will be displayed in the PowerShell console.

Bash curl:

#!/bin/bash

if [ $# -ne 1 ]; then
echo "Usage: $0 path/to/pdf/file.pdf"
exit 1
fi

filePath="$1"
invoiceId=$(basename "$filePath" .pdf)
fileName="$invoiceId.pdf"
token="[TOKEN]"
adapterName="[ADAPTER NAME]"
apiUrl="https://api.gaviti.com/v2/adapter/run/$adapterName"
headers='{"Content-Type": "application/json"}'

# Read the PDF file as raw binary data
pdfData=$(base64 "$filePath")

# Create the JSON object
jsonObject=$(printf '{"invoiceId": "%s", "fileName": "%s", "token": "%s", "data": "%s"}' "$invoiceId" "$fileName" "$token" "$pdfData")

# Send the JSON data to the API endpoint
curl -X POST -H "$headers" -d "$jsonObject" "$apiUrl" curl

To use the script, save it to a file (e.g., pdf-to-gaviti.sh), make it executable with chmod +x pdf-to-gaviti.sh, and then run it with the command ./pdf-to-gaviti.sh /path/to/pdf/file.pdf, replacing /path/to/pdf/file.pdf with the actual path to the PDF file you want to transform and send to the API.

The script reads the specified PDF file, extracts the invoice ID from the file name, transforms the file data to the specified JSON format, and sends the JSON data to the API endpoint using a POST request with the specified headers and authentication token. The response from the API will be displayed in the terminal.

Did this answer your question?