It seems like your browser didn't download the required fonts. Please revise your security settings and try again.

Attention

As of March 1, 2022, the legacy Barracuda Essentials Security, Compliance, and Complete editions are no longer available for purchase. Only existing customers can renew or add users to these plans.

Following October 30, 2022, the documentation and trainings will no longer be updated and will contain outdated information.

For more information on the latest Email Protection plans, see Barracuda Email Protection.

To update your bookmarks, see the following for the latest documentation and trainings:

Note that MSP customers should continue to follow Barracuda Essentials for MSPs.

Example - API Client in Python

  • Last updated on

The following is an example of how you can use the Barracuda Email Security Service 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 ServiceIf 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')