Prompt Chain

Connect ChatGPT to Azure Functions

OAuth-protected Azure Function middleware that connects GPT Actions to applications, with guidance on pre-processing API data and returning files.

Works with azureopenaimicrosoft graph

91
Spark score
out of 100
Updated 3 months ago
Version 1.0.0

Add to Favorites

Why it matters

Enable ChatGPT to interact directly with Azure Functions, acting as middleware to process and transform API responses.

Outcomes

What it gets done

01

Build an OAuth-protected Azure Function to bridge ChatGPT and external applications.

02

Pre-process API responses to overcome token limits or convert data formats.

03

Enable ChatGPT to return files (e.g., CSV, PDF) instead of just text.

04

Integrate with Microsoft Graph for file lookups in services like SharePoint.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/oai-gptmiddlewareazurefunction | bash

Steps

Steps in the chain

01
Create Azure Function App

Create an Azure Function app with the following settings: Runtime stack: Node.js, Operating system: Windows, Enable public access: on. Complete the deployment and click 'Go to Resource' to access the Function App.

02
Set up Authentication - Add Identity Provider

On the left-hand side menu of the Azure Function App, click on Authentication under Settings. Add identity provider, select Microsoft as identity provider, choose Workforce as tenant type, and create a new application.

03
Add Permissions to Application

On the permissions tab, click Add Permission and add Files.Read.All and Sites.ReadAll. This allows the application to read files for use with the Microsoft Graph Search API.

04
Grant User Impersonation Permission

Click on the enterprise application you created. On the main page, click 'View API Permissions'. Search for Microsoft Azure App Service in APIs my organization uses and find user_impersonation. Add it and request Admin Consent.

05
Copy API Scope

Within the enterprise application, click on 'Expose an API' under Manage. Copy the scope that was created (format: api://<insert-uuid>/user_impersonation) and save it as SCOPE.

06
Add Postman Redirect URI

Click on Authentication under Manage. Under the Web section, add the Postman redirect URI (https://oauth.pstmn.io/v1/callback) for testing.

07
Save Client ID and Tenant ID

Go to Overview. Copy the application (client) ID and directory (tenant) ID and save them as CLIENT_ID and TENANT_ID.

08
Create HTTP Trigger Function

Go back to your Function App and click Create Function. Choose HTTP trigger and select an Authorization Level. Click on the function you created and click Get Function URL to save for later testing.

09
Save Authentication Secret

Go to Configuration in the Function App. Show the value for MICROSOFT_PROVIDER_AUTHENTICATION_SECRET, copy it, and save it for later use.

10
Test Authentication in Postman

Hit the endpoint in Postman using OAuth settings: Grant Type: Authorization Code, Auth URL: https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/authorize, Token URL: https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token, Client ID, Client Secret, and Scope from previous steps.

11
Get New Access Token and Test Endpoint

Click Get New Access Token in Postman, then hit the endpoint saved earlier. Verify successful response: 'This HTTP triggered function executed successfully.'

12
Generate OpenAPI Spec

Generate an OpenAPI spec for your endpoint.

13
Configure GPT Action with OAuth

Paste the OpenAPI spec into the Actions section of a GPT and choose OAuth as the authentication type. Fill out the OAuth settings the same way as in Postman.

14
Copy GPT Callback URI

Once you save the action, copy the callback URI shown at the bottom of the GPT configuration.

15
Add Callback URI to Azure Function

Go back to your Function App in Azure Portal. Click on Authentication under Settings, then click your Entra application. Click Authentication under Manage, add a new Redirect URI under Web section, paste the callback URI from GPT, and click Save.

Overview

GPT Action Library (Middleware): Azure Function

What it does

Step-by-step instructions for building OAuth-protected Azure Function middleware that connects GPT Actions to applications, with options for VSCode or Azure Portal deployment.

How it connects

Use when you need middleware between ChatGPT and applications like SharePoint, when you need to pre-process API responses or convert them to digestible text, or when you need to return files instead of plain text. Skip if your APIs already return well-formatted responses under character limits.

Source README

GPT Action Library (Middleware): Azure Function

Introduction

This page provides an instruction & guide for developers building middleware to connect a GPT Action to a specific application. Before you proceed, make sure to first familiarize yourself with the following information:

This particular GPT Action provides an overview of how to build an Azure Function, MSFT's cloud-based function builder. This documentation helps a user set up an OAuth-protected Azure Function to connect to a GPT Action, and to a sample application.

Value + Example Business Use Cases

Value: Users can now leverage ChatGPT's natural language capability to connect directly to Azure Function. This can in a few ways:

  • 100k character limit in GPT Actions: users can use the middleware to pre-process the text response from an API. For example, you can use OpenAI’s API in the middleware to summarize the text before sending it back to ChatGPT.
  • Typically for actions, users are relying on the SaaS API to return text. You can convert the response for the vendor API into easily digestible text, and it can handle different data types such as structured and unstructured data.
  • It can return files instead of just text. This can be useful to surface CSV files for Data Analysis, or bring back an PDF file and ChatGPT will treat it like an upload.

Example Use Cases:

  • A user needs to look up files in Sharepoint, but needs a middleware app between ChatGPT and Sharepoint
  • A user has built several steps in a row in an Azure function, and needs to be able to kick off that process using ChatGPT

Application Information

Application Key Links

Check out these links from the application before you get started:

Application Prerequisites

Before you get started, make sure you go through the following steps in your application environment:

  • Azure Portal with access to create Azure Function Apps and Azure Entra App Registrations

Application Setup

Installing the app

You can read more about languages and deployment options for Azure Functions on the left hand side of the documentation here.

Option 1: Use VSCode

See Microsoft’s documentation here for how to deploy using VSCode. If you have familiarity with this approach, feel free to use it. 

Option 2: Directly in Azure Portal

See the documentation here for how to deploy using the Azure portal. We’ll walk through an example here step by step.

Part 1: Create Function
  1. Create an Azure Function app. I used the following settings but you can use anything you are comfortable with. Note that not every language / operating system allows for editing the functions in the console directly - the combination I chose below does. For my walkthrough, I left everything as default and made the selections below. The below settings work out of the box for the SharePoint Node.js solutions here and here.

    1. Basics

      1. Do you want to deploy code or container image?:  Code

      2. Runtime stack: Node.js

      3. Operating system: Windows

    2. Networking

      1. Enable public access: on (need this on to connect to the GPT)
  2. After completing the above, you’ll land on the “Deployments” page. Once the deployment completes (which should only take a few minutes) click on “Go to Resource” to go back to the Function App

You may get an error the first time you attempt this, click create again and it will likely work.

Part 2: Set up Auth
  1. On the left-hand side menu of the Azure Function App, click on Authentication under the Settings menu. 

    1. Add identity provider

    2. Select Microsoft as identity provider. 

    3. Workforce as tenant type

    4. Create a new application. The instructions are fairly similar if you are using an existing application, but it is easier to create a new application as it will have the callback URLs and the API exposed automatically using “Easy Auth”. You can read more about that here.

    5. Leave all the other settings on this page as the default, but feel free to change based on your internal guidelines.

    6. On the permissions tab, click Add Permission and add Files.Read.All and Sites.ReadAll, then Add. This allows this application to read files which is important in order to use the Microsoft Graph Search API. If you are not using this for the SharePoint solution here and here you can skip this.

  2. Once it is created, click on the enterprise application you just created (so, leave the Function App page and land on the Enterprise Application that you just spun up). We are now going to give it one more permission, to execute the Azure Function by impersonating the user logging into the application. See here for more details.

    1. On the main page, click “View API Permissions”

    2. Search for Microsoft Azure App Service in the APIs my organization uses and find user_impersonation 

    3. Add it, then you’ll need an Admin on Azure Portal to Grant Admin Consent.

  1. Within that enterprise application, Click on “Expose an API” on the left hand menu under Manage, then copy the scope that was created using the Copy to Clipboard button. The scope should look like “api://<insert-uuid>/user_impersonation”. Save this for later as SCOPE.

  2. Click on “Authentication” on the left hand menu under Manage

    1. Under the Web section, you’ll notice one callback URI was added automatically. Add the Postman redirect URI (https://oauth.pstmn.io/v1/callback) for testing.
  3. On the left-hand side, go to Overview. Copy the application (client) ID and and the directory (tenant) ID and save for later as CLIENT_ID and TENANT_ID.

Part 3: Set up Test Function
  1. Leave the page by going home and then back to your Function App.

  2. Click on Create Function. For this example, I’m going to develop it in the portal, but you can also use VSCode or another IDE.

    1. Choose HTTP trigger

    2. For Authorization Level, you can choose any key type you want.

      1. Note this may error out the first time, but it is likely the Function did create, do a refresh of the page to check.
  3. Click on the function you just created (You may need to click refresh to see it). Click on Get Function URL and save it to test in Postman. You will also use this when creating the OpenAPI spec later when you put it into the GPT.

  4. Go back to the function app and click on Configuration. Show the value for the MICROSOFT_PROVIDER_AUTHENTICATION_SECRET variable, copy it (click advanced edit to copy it), and save it for later.

  

At this point, you should have a test function created, and you should have saved a client id, tenant id, secret, scope, and function URL. You are now ready to test out the authentication in Postman

Part 4: Test Authentication in Postman
  1. Try to hit endpoint you created in Postman using those OAuth settings:

    1. Grant Type: Authorization Code

    2. Auth URL: https://login.microsoftonline.com/`TENANT_ID`/oauth2/v2.0/authorize

    3. Auth Token URL: https://login.microsoftonline.com/`TENANT_ID`/oauth2/v2.0/token

    4. Client ID: CLIENT_ID from step 7 above

    5. Client secret: MICROSOFT_PROVIDER_AUTHENTICATION_SECRET from step 11 above

    6. Scope: SCOPE from step 5 above

    7. Client credentials: Send client credentials in body

  2. You will need to click Get New Access Token, and then hit the endpoint you saved in step 10 above. If it was successful, you should get this response: ”This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.”

Part 5: Set up your Application on an Azure Function

This should be done separately and is specific to your app. See the Sharepoint Cookbook for an example of that.

Part 6: Set up ChatGPT
  1. Generate an OpenAPI spec for your endpoint. 

  2. Paste that into the Actions section of a GPT, and choose OAuth as the authentication type. Fill out the OAuth settings the same way you did for Postman above. 

  3. Once you save the action, you will see a callback URI at the bottom of the GPT configuration. Copy that URL, then go back to your Function App in the Azure Portal.

  4. Click on Authentication under Settings, then click on your Entra application.

  5. Once you are there, then click Authentication under the Manage section.

  6. Add a new Redirect URI under the Web section of that page, and paste in the Callback URI you got from step 16, then click Save. 

  7. Test out the GPT and it should work as expected.

ChatGPT Steps

Custom GPT Instructions

This is application specific. See Sharepoint Cookbook for an example

OpenAPI Schema

Once you've created a Custom GPT, copy the text below in the Actions panel. Have questions? Check out Getting Started Example to see how this step works in more detail.

Below is an example of what connecting to this Middlware might look like. You'll need to insert your application's & function's information in this section.

Authentication Instructions

Below are instructions on setting up authentication with this 3rd party application. Have questions? Check out Getting Started Example to see how this step works in more detail.

Pre-Action Steps

Before you set up authentication in ChatGPT, please take the following steps in the application.

Follow steps 2 & 4 above to setting up authentication

In ChatGPT

In ChatGPT, click on "Authentication" and choose "OAuth". Enter in the information below.

  • Client ID: see step 12 above
  • Client Secret: ditto
  • Authorization URL: ditto
  • Token URL: ditto
  • Scope: ditto
  • Token: ditto

Post-Action Steps

Once you've set up authentication in ChatGPT, follow the steps below in the application to finalize the Action.

See above for testing out this application

Are there integrations that you’d like us to prioritize? Are there errors in our integrations? File a PR or issue in our github, and we’ll take a look.

Step 1: Create Azure Function App

Create an Azure Function app with the following settings: Runtime stack: Node.js, Operating system: Windows, Enable public access: on. Complete the deployment and click 'Go to Resource' to access the Function App.

Step 2: Set up Authentication - Add Identity Provider

On the left-hand side menu of the Azure Function App, click on Authentication under Settings. Add identity provider, select Microsoft as identity provider, choose Workforce as tenant type, and create a new application.

Step 3: Add Permissions to Application

On the permissions tab, click Add Permission and add Files.Read.All and Sites.ReadAll. This allows the application to read files for use with the Microsoft Graph Search API.

Step 4: Grant User Impersonation Permission

Click on the enterprise application you created. On the main page, click 'View API Permissions'. Search for Microsoft Azure App Service in APIs my organization uses and find user_impersonation. Add it and request Admin Consent.

Step 5: Copy API Scope

Within the enterprise application, click on 'Expose an API' under Manage. Copy the scope that was created (format: api://<insert-uuid>/user_impersonation) and save it as SCOPE.

Step 6: Add Postman Redirect URI

Click on Authentication under Manage. Under the Web section, add the Postman redirect URI (https://oauth.pstmn.io/v1/callback) for testing.

Step 7: Save Client ID and Tenant ID

Go to Overview. Copy the application (client) ID and directory (tenant) ID and save them as CLIENT_ID and TENANT_ID.

Step 8: Create HTTP Trigger Function

Go back to your Function App and click Create Function. Choose HTTP trigger and select an Authorization Level. Click on the function you created and click Get Function URL to save for later testing.

Step 9: Save Authentication Secret

Go to Configuration in the Function App. Show the value for MICROSOFT_PROVIDER_AUTHENTICATION_SECRET, copy it, and save it for later use.

Step 10: Test Authentication in Postman

Hit the endpoint in Postman using OAuth settings: Grant Type: Authorization Code, Auth URL: https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/authorize, Token URL: https://login.microsoftonline.com/TENANT_ID/oauth2/v2.0/token, Client ID, Client Secret, and Scope from previous steps.

Step 11: Get New Access Token and Test Endpoint

Click Get New Access Token in Postman, then hit the endpoint saved earlier. Verify successful response: 'This HTTP triggered function executed successfully.'

Step 12: Generate OpenAPI Spec

Generate an OpenAPI spec for your endpoint.

Step 13: Configure GPT Action with OAuth

Paste the OpenAPI spec into the Actions section of a GPT and choose OAuth as the authentication type. Fill out the OAuth settings the same way as in Postman.

Step 14: Copy GPT Callback URI

Once you save the action, copy the callback URI shown at the bottom of the GPT configuration.

Step 15: Add Callback URI to Azure Function

Go back to your Function App in Azure Portal. Click on Authentication under Settings, then click your Entra application. Click Authentication under Manage, add a new Redirect URI under Web section, paste the callback URI from GPT, and click Save.

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.