How to connect to Serverless SQL pool created in Synapse Analytics workspace from Rest API

Venkata Akhil Kumar Gummadi 0 Reputation points
2023-10-24T05:34:36.5766667+00:00

I am trying to connect to Serverless SQL Pool created in synapse analytics workspace. I have created a service principle and provided contributor access to the synapse workspace and storage account. I am getting 500 error while querying the data from an rest api using python.

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
4,384 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 15,446 Reputation points
    2023-10-24T10:30:32.7666667+00:00

    The 500 error is an internal problem on the server side of your application.

    You may check this thread : https://learn.microsoft.com/en-us/answers/questions/547714/serverless-sql-pool-has-encountered-an-internal-er

    I will add the fact that adding a handling error part can give you more insights into what went wrong to check whether your query is correct and compatible with the Serverless SQL pool.

    Also here is a sample code to authenticate and make a REST API request to query data from the Serverless SQL pool:

    import requests
    import json
    
    # Azure app registration details
    tenant_id = "<Your Tenant ID>"
    client_id = "<Your Client ID>"
    client_secret = "<Your Client Secret>"
    
    # Get the Azure token
    token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
    token_data = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret,
        'scope': 'https://graph.microsoft.com/.default'
    }
    token_r = requests.post(token_url, data=token_data)
    token = token_r.json().get("access_token")
    
    # Synapse workspace details
    workspace_name = "<Your Workspace Name>"
    sql_endpoint = f"https://{workspace_name}.dev.azuresynapse.net"
    
    # SQL Query
    sql_query = "SELECT * FROM <Your Table>"
    
    # REST API request to query data
    headers = {
        'Authorization': f'Bearer {token}',
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    response = requests.post(f"{sql_endpoint}/query", headers=headers, json={"sql": sql_query})
    
    # Check response
    if response.status_code == 200:
        print(response.json())
    else:
        print(f"Error: Unable to fetch data. Status code: {response.status_code}")
        print(response.text)
    
    
    1 person found this answer helpful.