Select to view content in your preferred language

Generate Email When User's Credit Allowance is Low

503
5
01-22-2026 07:57 AM
bbywaterbw
Emerging Contributor

I have a several notebooks running that will fail when the user has 29 credits remaining. I believe this is the Esri default, because I cannot find a way around this even if the notebook uses a fraction of a credit.

My solution would be to generate an email to myself or another admin when a user has a low credit balance. Is this a possible trigger to set in Power Automate?

My only other approach would be to use a notebook to scrape user credit information on a regular basis and add that to a table that would in turn trigger a 'When a Feature Class is Added' response from Power Automate. This seems a bit clunky and obviously uses more credits in the process.

0 Kudos
5 Replies
Annie_K
Esri Contributor

You're correct that's a built-in failsafe in ArcGIS Online that you can't change. You might try something like the following: 

  1. Connect to your ArcGIS Online organization using the ArcGIS API for Python.
  2. Get the organization’s credit balance from the credits property.
  3. Set a threshold (e.g., 50 credits).
  4. Check the balance and compare it to the threshold.
  5. Send an email alert using Python’s smtplib (or another email service API like SendGrid, Outlook, Gmail).
  6. Schedule the notebook to run periodically (ArcGIS Online Notebooks can be scheduled if you have Advanced Notebooks enabled, or you can run it locally via a cron job/Task Scheduler).
0 Kudos
BobBooth1
Esri Regular Contributor

I have some code that checks the credit balance for an account I use for Notebook automation that I want to keep topped up with credits.

I run it on a VM, but you could also set it up to run periodically on your personal machine, or some other machine that's available.

 

from arcgis.gis import GIS
import smtplib
from email.message import EmailMessage

reportPage = ''

# Connect to the GIS 
myGIS = GIS(url="https://myAGOorg.maps.arcgis.com/", username='MYADMINUSERNAME', password="MYPASSWORD")
theUserAccount = myGIS.users.get(username='THEUSERNAME_HERE')

cm = myGIS.admin.credits
credit_status = "User has " + str(theUserAccount.availableCredits) + " credits."

if theUserAccount.availableCredits < 50:
    cm.allocate(theUserAccount, 1000)
    reportPage = reportPage + '\n' + "Allocated 1000 Credits"



# SMTP method:

SENDER = 'userName@myCompany.com'
RECIPIENTS = ['userName@myCompany.com']
MESSAGE_SUBJECT = 'Automated Report User Credits'

MESSAGE_BODY = """
User Credits Status:

{}

""".format(reportPage)

smtpserver = smtplib.SMTP("smtp.myCompany.com")
#smtpserver.login(SMTP_USER, SMTP_PASSWORD)

msg = EmailMessage()
msg.set_content(MESSAGE_BODY)
msg['Subject'] = MESSAGE_SUBJECT
msg['from'] = SENDER
msg['to'] = RECIPIENTS

try:
    smtpserver.send_message(msg)
except smtplib.SMTPException as err:
    print("Unable to send mail:", err)
finally:
    smtpserver.quit()

  This one sends out a notice via email using SMTP, but you can also send messages from the ArcGIS Org if you're an admin.  If you wanted you could skip the automatic topping off and just send a message that the account is low.

This is an example showing how to send from the org:

um = myGIS.users
uMiniList = []
uMiniList.append(myGISusername)

user = um.get(theUserName)
subject = 'Credits Low'
message = f'''Hi, {myGISusername},
Your credits are low. 
'''
um.send_notification(uMiniList,subject,message,type='email')

 

I don't think Notebooks will let you send emails via SMTP, but they should allow you to send notifications from the org.

 

BobBooth1
Esri Regular Contributor

This tutorial shows the steps for setting up a batch file and Windows Scheduled Task to run a Python script.

https://learn.arcgis.com/en/projects/schedule-automated-near-real-time-data-updates/

bbywaterbw
Emerging Contributor

Thank you both for the replies - I will try to implement one or both of these suggestions and report back with any successes!

Annie_K
Esri Contributor

Thanks @BobBooth1 and good luck with the project @bbywaterbw!

0 Kudos