How to use Co-Pilot via API

Jarrett Trapani 10 Reputation points
2025-02-14T16:20:24.53+00:00

Hi,

I am trying to figure out if it is possible to query co-pilot and grab the generated answer via api. What I mean by this is for example say in Jira or any Ticketing System that can make calls to an API is it possible to make a call to co pilot with some type of prompt or query and in turn receive the response. Then the response data would be used to update the Jira ticket or in general be used by a third party application. I would also like to be able to build applications in .net that also can query co pilot.

I am looking to see if this is possible,
Thank you.

Developer technologies | ASP.NET | ASP.NET API
Microsoft Copilot | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Raymond Huynh (WICLOUD CORPORATION) 3,955 Reputation points Microsoft External Staff Moderator
    2025-07-23T07:57:13.2633333+00:00

    Hi Jarett ,

    If you’re looking to interact with Copilot (or similar AI services) via an API so you can send prompts and get responses programmatically for use in tools like Jira or your own .NET applications, there are a couple of approaches you can consider:

    1. Microsoft Copilot API Access:

    At the moment, Microsoft Copilot itself doesn’t offer a public, direct API in the same way as some other AI services. However, Microsoft is actively expanding Copilot integrations, and you can find the latest information and developer resources here:

    https://developer.microsoft.com/en-us/copilot

    2. Using Azure OpenAI Service:

    If your goal is to use generative AI (like GPT-4) in your own apps, Microsoft’s Azure OpenAI Service provides a robust API for this. You can send prompts and receive responses, and it’s well-suited for integration with .NET or any system that can make HTTP requests.

    3. Example: Integrating with Bots (Advanced):

    If you want to build a bot that interacts with users and provides Copilot-like capabilities, you can use Azure Bot Service together with the Direct Line API. With this approach, your application can send messages to the bot and receive responses programmatically.

    For .NET developers, Microsoft offers detailed documentation and examples on how to connect your bot to clients using the Direct Line API. You can find step-by-step instructions here:

    Connect a bot to Direct Line - Bot Service | Microsoft Learn

    This resource will guide you through setting up your bot, generating Direct Line tokens, and handling message exchanges from your .NET applications.

    Hope this helps!

    1 person found this answer helpful.

  2. pavan kumar 0 Reputation points
    2025-02-15T19:12:34.22+00:00

    User's image

    #The below is for Azure Functions to invoke the copilot bot via code. understand Activities, Events, invoke received, invoked copilot triggers.

    import azure.functions as func
    import logging
    import requests
    import time
    import os
    import json
    
    # Load environment variables
    from dotenv import load_dotenv
    load_dotenv()
    
    TOKEN_ENDPOINT = os.getenv('TOKEN_ENDPOINT')
    BOT_SECRET = os.getenv('BOT_SECRET')
    
    # Global variables to store conversation state
    direct_line_token = None
    conversation_id = None
    
    app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
    
    @app.function_name(name='copilot_api')
    @app.route(route="copilot_api")  # Define the HTTP trigger route
    def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
        global direct_line_token, conversation_id
    
        logging.info('Python HTTP trigger function processed a request.')
    
        try:
            # Parse the JSON body sent by Bot Framework Emulator
            req_body = req.get_json()
            
            # Extract the user message (from "text" field)
            user_message = req_body.get('text')
    
            # Ensure a message is present in the request body
            if not user_message:
                return func.HttpResponse(
                    "User message is missing in the request body",
                    status_code=400
                )
    
            # Get a Direct Line token if not already available
            if not direct_line_token:
                direct_line_token = get_direct_line_token()
                if not direct_line_token:
                    return func.HttpResponse(
                        "Failed to obtain Direct Line token",
                        status_code=500
                    )
    
            # Start a new conversation if not already started
            if not conversation_id:
                conversation_id = start_conversation(direct_line_token)
                if not conversation_id:
                    return func.HttpResponse(
                        "Failed to start a conversation",
                        status_code=500
                    )
    
            # Send the user's message and get the bot's response
            bot_response = send_message_and_get_response(direct_line_token, conversation_id, user_message)
            if bot_response:
                return func.HttpResponse(bot_response)
            else:
                return func.HttpResponse(
                    "Failed to get a response from the bot",
                    status_code=500
                )
    
        except ValueError:
            return func.HttpResponse(
                "Invalid request body",
                status_code=400
            )
    
    
    # Function to get the Direct Line token
    def get_direct_line_token():
        headers = {
            'Authorization': f'Bearer {BOT_SECRET}'
        }
        try:
            response = requests.get(TOKEN_ENDPOINT, headers=headers)
            response.raise_for_status()
            data = response.json()
            return data.get('token')
        except requests.exceptions.RequestException as e:
            logging.error(f"Error fetching Direct Line token: {e}")
            if e.response is not None:
                logging.error(f"Error details: {e.response.text}")
            return None
    
    
    # Function to start a conversation
    def start_conversation(direct_line_token):
        headers = {
            'Authorization': f'Bearer {direct_line_token}',
            'Content-Type': 'application/json'
        }
        try:
            response = requests.post('https://directline.botframework.com/v3/directline/conversations', headers=headers)
            response.raise_for_status()
            data = response.json()
            return data.get('conversationId')
        except requests.exceptions.RequestException as e:
            logging.error(f"Error starting conversation: {e}")
            return None
    
    
    # Function to send a message and retrieve the bot's response
    def send_message_and_get_response(direct_line_token, conversation_id, message):
        headers = {
            'Authorization': f'Bearer {direct_line_token}',
            'Content-Type': 'application/json'
        }
        data = {
            'type': 'message',
            'from': {'id': 'user'},
            'text': message
        }
        try:
            send_response = requests.post(
                f'https://directline.botframework.com/v3/directline/conversations/{conversation_id}/activities',
                headers=headers, json=data
            )
            send_response.raise_for_status()
    
            # Poll for the bot's response
            activity_id = send_response.json()['id']
            timeout = 10
            start_time = time.time()
    
            while time.time() - start_time < timeout:
                get_response = requests.get(
                    f'https://directline.botframework.com/v3/directline/conversations/{conversation_id}/activities',
                    headers=headers
                )
                get_response.raise_for_status()
    
                activities = get_response.json()['activities']
                for activity in activities:
                    if (activity['type'] == 'message' and
                            activity['from']['id'] != 'user' and
                            'replyToId' in activity and
                            activity['replyToId'] == activity_id):
                        return activity['text']
    
                time.sleep(0.5)
    
            logging.warning("Timeout waiting for bot's response.")
            return None
    
        except requests.exceptions.RequestException as e:
            logging.error(f"Error interacting with Direct Line: {e}")
            if e.response is not None:
                logging.error(f"Error details: {e.response.text}")
            return None
    
    0 comments No comments

Your answer

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