{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Use Webhooks\n",
    "\n",
    "You may wish to use webhooks in your client, especially when using async streams in case you want to update something in your service once the API call to LangGraph Cloud has finished running. To do so, you will need to expose an endpoint that can accept POST requests, and then pass it to your API request in the \"webhook\" parameter.\n",
    "\n",
    "Currently, the SDK has not exposed this endpoint but you can access it through curl commands as follows.\n",
    "\n",
    "The following endpoints accept `webhook` as a parameter: \n",
    "\n",
    "- Create Run -> POST /thread/{thread_id}/runs\n",
    "- Create Thread Cron -> POST /thread/{thread_id}/runs/crons\n",
    "- Stream Run -> POST /thread/{thread_id}/runs/stream\n",
    "- Wait Run -> POST /thread/{thread_id}/runs/wait\n",
    "- Create Cron -> POST /runs/crons\n",
    "- Stream Run Stateless -> POST /runs/stream\n",
    "- Wait Run Stateless -> POST /runs/wait\n",
    "\n",
    "The following example uses a url from a public website that allows users to create free webhooks, but you should pass in the webhook that you wish to use. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "curl --request POST \\\n",
    "  --url http://localhost:8123/threads/b76d1e94-f251-40e3-8933-796d775cdb4c/runs/stream \\\n",
    "  --header 'Content-Type: application/json' \\\n",
    "  --data '{\n",
    "  \"assistant_id\": \"fe096781-5601-53d2-b2f6-0d3403f7e9ca\",\n",
    "  \"input\" : {\"messages\":[{\"role\": \"user\", \"content\": \"Hello! My name is Bagatur and I am 26 years old.\"}]},\n",
    "  \"metadata\": {},\n",
    "  \"config\": {\n",
    "    \"configurable\": {}\n",
    "  },\n",
    "  \"multitask_strategy\": \"reject\",\n",
    "  \"stream_mode\": [\n",
    "    \"values\"\n",
    "  ],\n",
    " \"webhook\": \"https://webhook.site/6ca33471-dd65-4103-a851-0a252dae0f2a\"\n",
    "}'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To check that this worked as intended, we can go to the website where our webhook was created and confirm that it received a POST request:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Webhook response](./img/webhook_results.png)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
