<jupyter_start><jupyter_text>![SAP Ariba logo](./images/sap-ariba-small.jpeg)    Ariba Network Invoice Header Data Extraction API overview OverviewAPI for extracting supplier invoices from the Ariba Network.**API Documentation**https://help.sap.com/viewer/9e361d7c0ccc40f5840012785f249811/cloud/en-US/e3f2b8c8a878417bb3ff40db62050f8d.html  Importing required packages<jupyter_code>import json
import requests
import pandas as pd
from pandas.io.json import json_normalize
from IPython.display import display<jupyter_output><empty_output><jupyter_text>Function retrieve authorization token<jupyter_code>def get_access_token(base64_auth):
    payload = {}
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Basic {base64_auth}"
    }
    
    response = requests.request("POST", OAUTH_API_URL, headers=headers, data=payload)
    
    return response.json()["access_token"]<jupyter_output><empty_output><jupyter_text>API details<jupyter_code># authentication server
OAUTH_API_URL = "https://api.ariba.com/v2/oauth/token?grant_type=client_credentials"

ANID = "ANXXXXXXXXXX-T" # replace by your own ANID

# API credentials

INV_EXTRACTOR_BASEURL = "https://openapi.ariba.com/api/invoice-extraction/v1/prod/invoices"
INV_EXTRACTOR_APIKEY = "XXXXXX" # replace by your own Key
INV_EXTRACTOR_BASE64 = "XXXXXXX" # replace by your own BASE64 secret

INV_EXTRACTOR_TOKEN = get_access_token(INV_EXTRACTOR_BASE64)
INV_EXTRACTOR_TOKEN<jupyter_output><empty_output><jupyter_text>Main Program<jupyter_code>api_headers = {
        "Authorization": f"Bearer {INV_EXTRACTOR_TOKEN}",
        "X-ARIBA-NETWORK-ID": ANID,
        "apiKey": INV_EXTRACTOR_APIKEY,
        "accept": "application/json",
    }

#
api_response = requests.get(
    INV_EXTRACTOR_BASEURL,
    headers=api_headers,
)


data = json.loads(api_response.text)["content"]

#flattening json
list_of_invoices = pd.json_normalize(data, max_level=1)

#transforming into DataFrame
invoices = pd.DataFrame(list_of_invoices)

#displaying invoices as a nice table html
display(invoices)

#convert to CSV (optional)
invoices.to_csv(r"invoice_test.csv")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10296846011131726, 'fraction_numerical': 0.025974025974025976, 'mean_word_length': 5.869426751592357, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '273164', 'license': 'Apache-2.0', 'n_tokens_mistral': 820, 'n_tokens_neox': 788, 'n_words': 130, 'path': 'topics/apis/recipes/retrieve-invoices-aribanetwork-api.ipynb', 'repo_name': 'dburla2/ariba-extensibility-samples', 'size': '2156'}