cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Accessing a FastAPI endpoint using Personal Access Token (PAT)

Aakash_Bhandari
New Contributor III

Hello Community,

I have a FastAPI endpoint on a cluster with addess 0.0.0.0:8084/predict. And I want to send a request to this endpoint from a React App which is locally hosted on my computer. I have a Personal access token for the workspace but dont know how to send the request using PAT. If anyone knows, please mention how can I hit

http://0.0.0.0:8084/predict

endpoint in a cluster so that I can do realtime prediction with low latency. Mentioning a URL template to hit would be a great help.

PS. My current requirement does not allow me to use Model serving feature of Databricks.

1 ACCEPTED SOLUTION

Accepted Solutions

Anonymous
Not applicable

@Aakash Bhandariโ€‹ :

To send a request from a React App to a FastAPI endpoint on a Databricks cluster using a Personal Access Token (PAT), you can use the requests module in Python to make HTTP requests.

Here's an example of how to use requests to send a POST request to your FastAPI endpoint:

import requests
 
url = "http://0.0.0.0:8084/predict"
headers = {"Authorization": "Bearer <your PAT here>"}
data = {"input": "your input data here"}
 
response = requests.post(url, headers=headers, json=data)
 
if response.status_code == 200:
    result = response.json()
    # do something with the result
else:
    print("Request failed with status code:", response.status_code)

Replace <your PAT here> with your actual Personal Access Token. You can generate a PAT in your Databricks workspace by going to User Settings > Access Tokens and creating a new token with the appropriate permissions.

In this example, the data parameter is a dictionary containing the input data that you want to send to your endpoint. You can modify this dictionary to include any additional parameters that your endpoint requires.

Also, note that the headers parameter includes an Authorization header with the value "Bearer <your PAT here>". This header is required for authenticating with your Databricks cluster using the PAT.

Make sure to modify the url parameter to match the actual address of your FastAPI endpoint. If your Databricks cluster is behind a firewall or network security group, you may need to configure your firewall rules to allow traffic from your local computer to the cluster's IP address and port number.

Hope all these suggestions help you to come to the solution.

View solution in original post

6 REPLIES 6

Anonymous
Not applicable

@Aakash Bhandariโ€‹ :

To send a request from a React App to a FastAPI endpoint on a Databricks cluster using a Personal Access Token (PAT), you can use the requests module in Python to make HTTP requests.

Here's an example of how to use requests to send a POST request to your FastAPI endpoint:

import requests
 
url = "http://0.0.0.0:8084/predict"
headers = {"Authorization": "Bearer <your PAT here>"}
data = {"input": "your input data here"}
 
response = requests.post(url, headers=headers, json=data)
 
if response.status_code == 200:
    result = response.json()
    # do something with the result
else:
    print("Request failed with status code:", response.status_code)

Replace <your PAT here> with your actual Personal Access Token. You can generate a PAT in your Databricks workspace by going to User Settings > Access Tokens and creating a new token with the appropriate permissions.

In this example, the data parameter is a dictionary containing the input data that you want to send to your endpoint. You can modify this dictionary to include any additional parameters that your endpoint requires.

Also, note that the headers parameter includes an Authorization header with the value "Bearer <your PAT here>". This header is required for authenticating with your Databricks cluster using the PAT.

Make sure to modify the url parameter to match the actual address of your FastAPI endpoint. If your Databricks cluster is behind a firewall or network security group, you may need to configure your firewall rules to allow traffic from your local computer to the cluster's IP address and port number.

Hope all these suggestions help you to come to the solution.

Thank you @Suteja Kanuriโ€‹ . Previously, I was using context create and command run APIs before for the work around.

Anonymous
Not applicable

@Aakash Bhandariโ€‹ : I am glad my resolution helped you! Cheers~~Happy progressing.

Hello @Suteja Kanuriโ€‹ 

Following up on this, How would the request module know the URL request is for databricks. If we try to hit the url

http://0.0.0.0:8080

It will look into the computer's localhost to get the service running in 8080 port. Is there a way to address this?

Anonymous
Not applicable

@Aakash Bhandariโ€‹ :

You are correct that if you try to hit http://0.0.0.0:8080 from your React App, it will look for the service running on the local machine's localhost and not on the Databricks cluster.

To address this, you need to specify the URL of the cluster's FastAPI endpoint in the request. This URL will typically be of the form https://<databricks-instance-name>.cloud.databricks.com:443/<path-to-your-endpoint>.

Here's an example code snippet that shows how to make a POST request to a FastAPI endpoint hosted on a Databricks cluster:

import requests
import json
 
# Replace <databricks-instance-name> with the name of your Databricks instance
url = "https://<databricks-instance-name>.cloud.databricks.com:443/predict"
 
# Replace <token> with your Databricks Personal Access Token
headers = {"Authorization": f"Bearer <token>"}
 
# Example payload for the POST request
payload = {"data": {"age": 35, "income": 45000}}
 
# Send the request to the FastAPI endpoint
response = requests.post(url, headers=headers, data=json.dumps(payload))
 
# Print the response
print(response.json())

Note that you need to replace <databricks-instance-name> with the name of your Databricks instance and <token> with your Databricks Personal Access Token. You also need to replace the example payload with your own JSON payload for the POST request.

Hey @Suteja Kanuriโ€‹ ,

I implemented your code in my usecase but it is throwing 404 error as reponse. Below is the code I am using and dont know why it is not working.

header = {"Authorization": "Bearer " + os.getenv("PAT")}
context_response = requests.get(
    "https://<dtaabricks-instance>.gcp.databricks.com:443/api_health",
    headers=header
)
 
context_data = context_response.json()
print(context_data)

I am running my fastAPI app in a notebook in one of the cluster. The app is running on port 8081. Could you please help me out where am I getting wrong here?

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group