Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device.
Atlassian cookies and tracking notice, (opens new window)
Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
/
Example - API Client in Python
Published Mar 13

Example - API Client in Python

Mar 13, 2026
Subscribe

The following is an example of how you can use the Email Gateway Defense API written in Python.

Before invoking the Python script, you must export the following variables with the Client ID and Client Secret that you obtained from the Barracuda Token Service. If you have not already registered your application using the Barracuda Token Service to obtain a Client ID and Client Secret, see Register your Application in the Barracuda Token Service.

export API_CLIENT_ID=xxxx

export API_CLIENT_SECRET=xxx

 

import requests import json import os import time class essAPI(): host = None client_id = None client_secret = None access_token = None access_token_expiration = None def __init__(self, host, client_id, client_secret): self.host = host self.client_id = client_id self.client_secret = client_secret try: self.access_token = self.getAccessToken() if self.access_token is None: raise Exception("Request for access token failed") except Exception as e: print(e) else: self.access_token_expiration = int(time.time()) + 3500 def getAccessToken(self): try: data = { 'grant_type': 'client_credentials', 'scope': 'ess:account:read' } req = requests.post(self.host, data=data, auth=(self.client_id, self.client_secret)) req.raise_for_status() except Exception as e: print(e) return None else: return req.json()['access_token'] def __str__(self): return "host: {} client_id: {} client_secret: {} access_token_expiration: {}".format(self.host, self.client_id, self.client_secret, self.access_token_expiration) def refreshToken(decorated): def wrapper(api, *args, **kwargs): if int(time.time()) > api.access_token_expiration: api.getAccessToken() return decorated(api, *args, **kwargs) return wrapper @refreshToken def callEssApi(api, path): try: print('---------------------------------- %s ----------------------------------', path) api_headers = {} api_headers['Authorization'] = 'Bearer ' + api.access_token # print(api_headers) r = requests.get('https://api.barracudanetworks.com/beta' + path, headers=api_headers) r.raise_for_status() except Exception as e: print("ESS API call failed for {}: {}", path, e) else: print(json.dumps(r.json(), indent=4)) # End of boilerplate code host = os.environ.get('TOKEN_URL') or 'https://login.bts.barracudanetworks.com/token' client_id = os.environ.get('API_CLIENT_ID') client_secret = os.environ.get('API_CLIENT_SECRET') def lookupAccount(account_id): api = essAPI(host, client_id, client_secret) # print(api) callEssApi(api, '/accounts/ess') callEssApi(api, '/accounts/{}/ess/domains'.format(account_id)) callEssApi(api, '/accounts/{}/ess/domains/address.com'.format(account_id)) callEssApi(api, '/accounts/{}/ess/statistics'.format(account_id)) # Replace the below with your account_id lookupAccount('xxxxxxx')

We value your feedback.
If you have questions, suggestions, or feedback on our documentation, contact the Campus Product Documentation team.
For general product inquiries or technical support, please contact the global Barracuda Support team.

🔍 Discover Our Product Documentation

Email Gateway Defense
Results will update as you type.
  • Release Notes
  • Upcoming Change with Admin Sender Policies to Override User Policies
  • How to Protect Against Gateway Bypass and Direct Send Risks
  • Getting to Know Email Gateway Defense
  • Getting Started
  • Moving from Barracuda Email Security Gateway to Email Gateway Defense
  • How the Service Works
  • Email Gateway Defense IP Ranges Used for Configuration
  • Partner Accounts
  • Sender Policy Framework for Outbound Mail
  • How to Set Up MX Records for Domain Verification
  • Security Warnings
  • Understanding User Accounts
  • Sender Authentication
  • Inbound Filtering Policy
  • Outbound Filtering Policy
  • Advanced Configuration
  • Managing Domains
  • Logs and Reports
  • User Guides
  • Tools and Add-Ins
  • API Overview
    • Getting Started with the API
    • Registering your Application in the Barracuda Token Service
    • Obtaining an Access Token
    • Resources
    • Paging
    • Example - API Client in Python
  • Troubleshooting, Error Messages, and Additional Tips
  • Known Behaviors and FAQs
  • Online Service Terms

    You‘re viewing this with anonymous access, so some content might be blocked.
    {"serverDuration": 11, "requestCorrelationId": "4228dfa0fdff4808bcb4a61a836f5f0a"}