admin08077's picture
Update app.py
543d6c9 verified
import streamlit as st
import requests
import base64
# Title of the app
st.title("Modern Treasury API Integration")
st.subheader("Made by Citibank Demo Business Inc.")
# Sidebar for API key and Organization ID input
st.sidebar.header("API Configuration")
api_key = st.sidebar.text_input("Enter your Modern Treasury API Key", type="password")
organization_id = st.sidebar.text_input("Enter your Organization ID")
# Select the API endpoint the user wants to interact with
endpoint_choice = st.selectbox("Choose API Endpoint", [
"Payment Orders",
"Expected Payments",
"Returns",
"Incoming Payment Details",
"Counterparties",
"Internal Accounts",
"Ledgers",
"Ledger Accounts"
])
# Base URL for Modern Treasury API
base_url = "https://app.moderntreasury.com/api/"
# Define API endpoints including Ledgers and Ledger Accounts
MODERN_TREASURY_ENDPOINTS = {
"Payment Orders": "payment_orders",
"Expected Payments": "expected_payments",
"Returns": "returns",
"Incoming Payment Details": "incoming_payment_details",
"Counterparties": "counterparties",
"Internal Accounts": "internal_accounts",
"Ledgers": "ledgers",
"Ledger Accounts": "ledger_accounts"
}
# Function to create the Basic Auth header by encoding the Organization ID and API Key
def create_basic_auth_header(org_id, api_key):
# Combine Organization ID and API Key, separated by a colon
credentials = f"{org_id}:{api_key}"
# Encode the credentials in base64
base64_credentials = base64.b64encode(credentials.encode()).decode('utf-8')
# Return the full Authorization header
return f"Basic {base64_credentials}"
# Function to fetch data from the selected Modern Treasury API endpoint
def fetch_data(api_key, org_id, endpoint):
url = base_url + MODERN_TREASURY_ENDPOINTS[endpoint]
# Create the Basic Auth header
headers = {
"Authorization": create_basic_auth_header(org_id, api_key)
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json()
else:
st.error(f"Error: {response.status_code} - {response.text}")
return None
# Main logic to fetch and display data
if st.button("Fetch Data"):
if api_key and organization_id:
data = fetch_data(api_key, organization_id, endpoint_choice)
if data:
st.json(data)
else:
st.error("Please provide both a valid API key and Organization ID.")
# Display footer
st.sidebar.markdown("Powered by Streamlit and Modern Treasury API")
st.sidebar.markdown("Made by Citibank Demo Business Inc.")