Unable to Create Alert Using API

zg
New Contributor III

Hi All, I'm trying to create an alert using the Databricks REST API, but I keep encountering the following error:
Error creating alert: 400 {"message": "Alert name cannot be empty or whitespace"}:


{
"alert": {
"seconds_to_retrigger": 0,
"display_name": "AlertTest",
"condition": {
"op": "EQUAL",
"operand": {
"column": {
"name": "Id"
}
},
"threshold": {
"value": {
"double_value": 1
}
}
},
"query_id": "123e4567-e89b-12d3-a456-426614174000", 
"parent_path": "/Workspace/Users/user@mail.com"
}
}

Alberto_Umana
Databricks Employee
Databricks Employee

Hi @zg,

Could you please share the REST API Endpoint you are making the request to?

zg
New Contributor III

Hello @Alberto_Umana,

Thank you for your response. The REST API Endpoint I am using for this request is:
https://<databricks-workspace>.azuredatabricks.net/api/2.0/preview/sql/alerts.

Please let me know if you need additional details.

Best regards

filipniziol
Esteemed Contributor

Hi @zg ,

You are sending the payload related to the new endpoint (/api/2.0/sql/alerts) to the old endpoint (/api/2.0/preview/sql/alerts).

That are the docs of the old endpoint:
https://docs.databricks.com/api/workspace/alertslegacy/create
As you can see the field is called "name"

filipniziol_0-1738058526972.png

In the new endpoint the name is called "display_name":
https://docs.databricks.com/api/workspace/alerts/create

To sum up, just change your endpoint from /api/2.0/preview/sql/alerts to /api/2.0/sql/alerts and it should work.

 

View solution in original post

zg
New Contributor III

You're absolutely right! Even though I had tried it before, it didn’t work initially. However, I resolved the problem by reviewing the API's structure using the UI. By following the same structure and defining a new alert condition, it worked without any errors. Here's the code I used:

import requests
import json

# Databricks API Details
HOST = "https://<databricks-workspace>.azuredatabricks.net"
TOKEN = "token"
ENDPOINT = f"{HOST}/api/2.0/sql/alerts"

# Function to check the alert API
def check_alert_api():
headers = {
"Authorization": f"Bearer {TOKEN}"
}
response = requests.get(ENDPOINT, headers=headers)
print("Response Data:", json.dumps(response.json(), indent=4))

# Execute the function
check_alert_api()

View solution in original post