cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Administration & Architecture
Explore discussions on Databricks administration, deployment strategies, and architectural best practices. Connect with administrators and architects to optimize your Databricks environment for performance, scalability, and security.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Databricks Apps with FastAPI

AndyK
New Contributor II

I have a FastAPI Databricks App deployed to a hosted workspace. How can I call the API from external tools like Postman?

P.S. I was able to call the API within the same browser.

6 REPLIES 6

SP_6721
Contributor III

Hi @AndyK ,

External tools donโ€™t share browserโ€™s login context, so youโ€™ll need to add a bearer token in the Authorization header manually.

Start by generating an access token. You can either use the Databricks CLI (databricks auth token) to get an OAuth token, or create a Personal Access Token (PAT) from your workspace settings.
Then in Postman, enter your API endpoint like:
https://<workspace-host>/api/<endpoint-path>
Go to the Authorization tab, choose Bearer Token, and paste your token.

Make sure you have CAN USE permission on the Databricks App. Also, note that tokens,especially OAuth, can expire (usually in about an hour), so you might need to generate new one when needed

AndyK
New Contributor II

Hi,

PAT doesn't seem to work, but we use a workspace token and it was able to go through. Thanks

How to generate one?

I tried generating, the token this for service principle:

export CLIENT_ID=<client-id>
export CLIENT_SECRET=<client-secret>

curl --request POST \
--url <token-endpoint-URL> \
--user "$CLIENT_ID:$CLIENT_SECRET" \
--data 'grant_type=client_credentials&scope=all-apis'

It did generates a token, but that token still doesn't work with the Databricks App i created. 
I am just passing it like:

response = requests.get(
"https://<app specific url>.azure.databricksapps.com/api/v1/table",
headers={"Authorization": f"Bearer {token} "})
print(response.json())

Gareema
Contributor

What do you mean by workspace token? Can you elaborate a little?

AndyK
New Contributor II

Hi,

Make sure your token endpoint url is like this: https://<databricks-instance>/oidc/v1/token.

Refer here for workspace-level access token: https://docs.databricks.com/aws/en/dev-tools/auth/oauth-m2m

Also make sure you have granted access of the service principal to the app: https://apps-cookbook.dev/docs/fastapi/getting_started/connections/connect_from_app 

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local communityโ€”sign up today to get started!

Sign Up Now