Submitting invoice images using Python

Although Python is not officially supported, this code example can help you get started, if you need to create a custom integration using Python. The code below demonstrates how to authenticate a user and upload an invoice image file for processing in Tungsten AP Essentials. In order to run the code, you must supply:

  • An API key from a partner account.
  • A user name and password from the partner account or sub-account.
  • The ID of the customer that you want to send the invoice to. You can obtain the customer ID via the API using GetCurrentCustomer or GetCurrentUserCustomers, for example.
  • The ID of the buyer. If the account does not have a buyer specified, use the customer ID. You can obtain the buyer ID via the API using GetCurrentBuyer or GetCurrentUserBuyers, for example.
  • The System name of the document type that you want to use to process the invoice, if your solution uses more than one document type.

    # Python version: 3.4.3
    # Requests library version: 2.6.0 http://docs.python-requests.org/
     
    import requests
     
    APIKey = 'YOUR_API_KEY'
    userName = 'YOUR_USER_NAME'
    password = 'YOUR_PASSWORD'
    
    customerId = 'YOUR_CUSTOMER_ID'
    buyerId = 'YOUR_BUYER_ID'
    documentType = 'YOUR_DOC_TYPE'
    
    # Make sure you use the base URL that corresponds to your production system:
    # Europe - https://services.readsoftonline.com
    # Australia - https://services-au.readsoftonline.com
    # North America - https://services-us.readsoftonline.com
    url = 'https://services.readsoftonline.com:443/authentication/rest/authenticate'
    payload = '<AuthenticationCredentials><UserName>'+userName+'</UserName><Password>'+password+'</Password><AuthenticationType>SetCookie</AuthenticationType></AuthenticationCredentials>'
    headers = {'Accept': 'application/xml',
    'Content-Type': 'application/xml; charset=UTF-8',
    'x-rs-version': '2011-10-14',
    'x-rs-key': APIKey,
    'x-rs-culture': 'en-EN',
    'x-rs-uiculture': 'en-EN'}
    # Use a session to persist the authentication cookie across requests.
    s = requests.Session()
    # Post an authentication request.
    req = s.post(url, data=payload, headers=headers)
    # If status code 200 is returned, the authentication succeeded.
    print(req.status_code)
    print('')
    print(req.text)
    print('')
    
    # Prepare the header for posting the image.
    headers = {
    'content-type': 'application/octet-stream',
    'x-rs-version': '2011-10-14',
    'x-rs-key': APIKey,
    }
    # Get the image data.
    with open(r'C:\temp\invoice.png', "rb") as imageData:
     imageData = imageData.read()
    url = 'https://services.readsoftonline.com:443/files/rest/image2?filename=test.png&customerid='+customerId+'&batchexternalid=123456&buyerid='+buyerId+'&documenttype='+documentType sortingmethod=OneDocumentPerFile'
    # Post the invoice image to Tungsten AP Essentials.
    req = s.post(url, data=imageData, headers=headers)
    print(req.status_code)
    print('')
    print(req.text)