Incoming webhook trigger

An incoming webhook trigger will enhance workflow efficiency and seamlessly integrate with external tools. The Tailor PF application creates the API endpoint when this trigger is defined.

Tutorial steps

To create an incoming webhook trigger, you'll need to:

  1. Activate the Executor service
  2. Add the trigger in executor.cue manifest file
  3. Apply the change using tailorctl
  4. Verify the trigger

1. Activate the Executor service

To activate the Executor service, add executor service to the list of services in the workspace.cue file as the following.

<span><span style="color: var(--shiki-token-keyword)">package</span><span style="color: var(--shiki-color-text)"> v2</span></span>
<span></span>
<span><span style="color: var(--shiki-token-keyword)">import</span><span style="color: var(--shiki-color-text)"> (</span></span>
<span><span style="color: var(--shiki-color-text)">	...</span></span>
<span><span style="color: var(--shiki-color-text)">	</span><span style="color: var(--shiki-color-text)">&quot;</span><span style="color: var(--shiki-token-string-expression)">tailor.build/template/manifest/services/executor</span><span style="color: var(--shiki-color-text)">&quot;</span></span>
<span><span style="color: var(--shiki-color-text)">)</span></span>
<span></span>
<span><span style="color: var(--shiki-color-text)">v2.#Workspace </span><span style="color: var(--shiki-token-keyword)">&amp;</span><span style="color: var(--shiki-color-text)"> {</span></span>
<span><span style="color: var(--shiki-color-text)">	Apps: [</span></span>
<span><span style="color: var(--shiki-color-text)">		application</span></span>
<span><span style="color: var(--shiki-color-text)">	]</span></span>
<span><span style="color: var(--shiki-color-text)">	Services: [tailordb</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> pipeline</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> auth</span><span style="color: var(--shiki-token-punctuation)">,</span><span style="color: var(--shiki-color-text)"> executor]</span></span>
<span><span style="color: var(--shiki-color-text)">}</span></span>
<span></span>

2. Add the trigger in executor.cue manifest file

package executor

import (
	"github.com/tailor-platform/tailorctl/schema/v2/executor"
	"tailor.build/template/manifest/services/pipeline:settings"
	"tailor.build/template/environment"
)

executor.#Spec & {
	Executors: [
		#incomingWebhookBasedExecutor,
	]
}

#incomingWebhookBasedExecutor: executor.#Executor & {
	Name:    "incomingwebhook-executor"
	Trigger: executor.#TriggerIncomingWebhook
	Target: executor.#TargetTailorGraphql & {
		AppName: environment.#app.namespace
		Invoker: settings.adminInvoker
		Query: """
			mutation createProduct($name: String!) {
				createProduct(input: { name: $name }) {
					id
				}
			}"""
		Variables: common.#Script & {
			Expr: """
			({
				"name": args.data.title,
			})"""
		}
	}
}

3. Apply the change using tailorctl

Run the following command to apply the changes.

<span><span style="color: var(--shiki-token-comment)"># Generate new cue files replacing the variables</span></span>
<span><span style="color: var(--shiki-token-function)">tailorctl</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">manifest</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">tidy</span></span>
<span><span style="color: var(--shiki-token-function)">cue</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">eval</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">-f</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">manifest/workspace.cue</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">-o</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">generated/workspace.cue</span></span>
<span></span>
<span><span style="color: var(--shiki-token-comment)"># Apply Executor</span></span>
<span><span style="color: var(--shiki-token-function)">tailorctl</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">workspace</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">apply</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">-m</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">generated/workspace.cue</span></span>
<span></span>

4. Verify the trigger

To verify the trigger follow the steps below.

  1. Get the secret for the incoming webhook
curl --request POST \
  --url https://api.tailor.tech/operator.v1.OperatorService/ListExecutorExecutors \
  --header 'Authorization: Bearer {access_token}' \
  --header 'Content-Type: application/json' \
  --data '{
	"workspace_id": "${your_workspace_id}"
  }'

To get the access token run the following command and copy the controlplaneaccesstoken.

cat ~/.tailorctl/config

Upon successful request you'll get the following response.

{
   "executors":[
      {
      "name": "incomingwebhook-executor",
      "triggerType": "EXECUTOR_TRIGGER_TYPE_INCOMING_WEBHOOK",
      "triggerConfig": {
        "incomingWebhook": { "secret": "{your_secret}" }
      },
      "targetType": "EXECUTOR_TARGET_TYPE_TAILOR_GRAPHQL",
      "targetConfig": {
        "tailorGraphql": {
          "appName": "{your_app_name}",
          "query": "\t\t\tmutation createProduct($name: String!) {\n\t\t\t\tcreateProduct(input: { name: $name }) {\n\t\t\t\t\tid\n\t\t\t\t}\n\t\t\t}",
          "variables": {
            "expr": "\t\t\t({\n\t\t\t\t\"input\": {\"name\": args.data.title},\n\t\t\t})"
          }
        }
      }
    },
   ]
}
  1. Construct the webhook URL

You can construct the URL by using the information from the executor.

https://api.tailor.tech/v1/executor/workspaces/{your_workspace_id}/executors/{name_of_the_executor}/invokeIncomingWebhook/{your_secret}
  1. Send a request the webhook URL
curl -X POST "https://api.tailor.tech/v1/executor/workspaces/{your_workspace_id}/executors/{name_of_the_executor}/invokeIncomingWebhook/{your_secret}" \
     -H "Content-Type: application/json" \
     -d '{"title": "Black shirt"}'
  1. Confirm the creation of the product

You can open the GraphQL playground to verify that the product is added to Tailor DB.

<span><span style="color: var(--shiki-token-comment)"># Open the GraphQL playground in the browser</span></span>
<span><span style="color: var(--shiki-token-function)">tailorctl</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">workspace</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">app</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">open</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">-n</span><span style="color: var(--shiki-color-text)"> </span><span style="color: var(--shiki-token-string)">inventory-tracker</span></span>
<span></span>

Run the following query to view all the products.

query {
  products{
    collection {
      id,
      name,
    }
  }
}