How to use Managed Identity to call a azure function from Azure SQL Database using sp_invoke_external_endpoint

Ayush Shrivastava 60 Reputation points
2024-08-26T13:45:30.0333333+00:00

How to use Managed Identity to call a azure function from Azure SQL Database using sp_invoke_external_endpoint.

When I try to create a credential using this query

CREATE

And then run my rest endpoint to call the Azure function

EXEC sp_invoke_external_rest_endpoint
  @url = N'https://{appName}.azurewebsites.net/api/{functionName}',
  @method = 'POST',
  @credential = {name},
   @payload =  @json_body, 
  @response = @response OUTPUT;

I shows me this error

SQL Error [31630] [S0001]: The database scoped credential '{name}' cannot be used to invoke an external rest endpoint.
Azure SQL Database
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,890 questions
{count} votes

Accepted answer
  1. Nandan Hegde 32,026 Reputation points MVP
    2024-08-30T04:23:29.97+00:00

    Refer the below aspect :

    Creating a database scope cred with random name :

    User's image

    Failure :

    User's image

    Update the cred as per proper URL name :

    User's image


2 additional answers

Sort by: Most helpful
  1. Sai Raghunadh M 150 Reputation points Microsoft Vendor
    2024-08-29T06:16:20.9766667+00:00

    Hi @ Ayush Shrivastava,

    Thanks for the question and using MS Q&A platform.

    You created a special username and password called a "database scoped credential" that allows your Azure SQL Database to access your Azure Function.

    To use this username and password in your call to sp_invoke_external_rest_endpoint, you need to specify the name of the credential in the @credential parameter of the stored procedure. In your case, you named the credential "auth".

    So, to use the credential in your call to sp_invoke_external_rest_endpoint, you can replace {name} in your query with the name of the database scoped credential you created, which is "auth". (@credential = {auth} This will allow your Azure SQL Database to authenticate with your Azure Function and call it successfully.

    For your better understanding please go through this Document: sp_invoke_external_rest_endpoint (Transact-SQL)

    Hope this helps. Do let us know if you have any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful.


  2. Nandan Hegde 32,026 Reputation points MVP
    2024-08-29T15:59:12.3633333+00:00

    Hello Ayush

    Apologies to have missed out on this thread.

    assuming that you have followed all the steps w.r.t app creation to access provision as stated below :

    https://github.com/Azure-Samples/azure-sql-db-invoke-external-rest-endpoints/blob/main/azure-functions.ipynb

    The above error comes in case if the database scoped credential name is other then the URL.

    Try having the databasecope cred name as below :

    [https://{appName}.azurewebsites.net/api/{functionName}]


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.