Can addNetworkDomain be chosen by the user of the pack?

Hi there,

I’m creating my first pack (yay :slight_smile: ), I’m not a developer but managed to get it to do what I wanted…

However if I want this pack to potentially also be useful for others, I’d need to figure out 2 things.

But first, what does the pack do?
This pack allows to add a button that triggers a webhook in Thrive Automator (a WordPress automation plugin).

The Thrive Automator plugin provides the webhook URL to use in the POST request BUT this also means that the webhook is specific to the WordPress domain and that’s where my first issue comes in:

  1. Because this Pack uses “fetcher.fetch”, specify the domain that your Pack makes HTTP requests to using "pack.addNetworkDomain(‘example.com’)
    ==> I can do this manually for my own pack and code, but I would like for this to be dynamic and based on the domain that the user fills out (as this would be their own self hosted WP website).
    I tried using a parameter but that’s not the way to go.

I read in the documentation:

There are services however where each account is associated with a distinct domain, instead of a sub-domain of a common root domain. This makes it impossible to declare them ahead of time as network domains. In these cases you can omit network domain declaration from your Pack, which will allow it to make requests to the account’s endpoint URL (and only that URL) regardless of domain.

And I feel the answer is in there BUT I can’t seem to figure it out (as there is no other authentification or token or anything needed)

So ideally, I would somehow give the user the option to enter the enpoint URL themselves and use that for the addNetworkDomain.

Is this possible? Any pointers as to how?

  1. I would like for parameters to be optional for the user. Is that possible? Now it seems like all parameters are required for it to work.

Thanks!

Hi @Hanne - Welcome to the community, and excited that you’re working on a Pack! The paragraph in the documentation you quoted is in the section talking about endpoint URLs, which is a feature of our authentication system. As you mentioned, you don’t need user authentication for your API, but that’s currently the only way to allow for a user-defined endpoint.

There is a workaround that you could try using however. The Custom authentication scheme allows for you to define which tokens / values to collect from the user when they sign in, and we allow you to not set any at all. Combining this with the endpoint feature you can build a solution where the user signs in to your Pack, but only needs to provide their base URL.

Here is some sample code to illustrate the approach:

import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();

pack.setUserAuthentication({
  type: coda.AuthenticationType.Custom,
  params: [],
  requiresEndpointUrl: true,
  getConnectionName: async function (context) {
    return context.endpoint;
  },
});

pack.addFormula({
  name: "HttpStatus",
  description: "Gets the status of a URL.",
  parameters: [
    coda.makeParameter({
      type: coda.ParameterType.String,
      name: "url",
      description: "The URL to check.",
    }),
  ],
  resultType: coda.ValueType.Number,
  execute: async function ([url], context) {
    let response = await context.fetcher.fetch({
      method: "HEAD",
      url: url,
    });
    return response.status;
  },
});

When installing the Pack, the user will be prompted to sign in, providing an endpoint URL:

image

Using that account they can connect to any URL with that prefix:

Let me know if you think this would work for your case.

Seems to be REALLY close :slight_smile:

The “Authentication” works, I didn’t get the error about having to provide the addNetworkDomain anymore.

However, the webhook is not getting triggered anymore :frowning:

I’m getting the following error code

Thrive Automator Webhook Trigger error
Couldn’t find this action in Thrive Automator Webhook Trigger

This is my current code:

import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();

pack.setUserAuthentication({
  type: coda.AuthenticationType.Custom,
  params: [],
  requiresEndpointUrl: true,
  getConnectionName: async function (context) {
    return context.endpoint;
  },
});

// Action formula (for buttons and automations) that adds a webhook trigger for Thrive Automator
pack.addFormula({
  name: "TriggerThriveAutomator",
  description: "This will trigger the webhook in Thrive Automator",
  parameters: [
     coda.makeParameter({
      type: coda.ParameterType.String,
      name: "WebhookURL",
      description: "Copy the webhook URL here",
    }),
    coda.makeParameter({
      type: coda.ParameterType.String,
      name: "key1",
      description: "Value in Key 1",
    }),
     coda.makeParameter({
      type: coda.ParameterType.String,
      name: "key2",
      description: "Value in Key 2",
    }),
     coda.makeParameter({
      type: coda.ParameterType.String,
      name: "key3",
      description: "Value in Key 3",
    }),
     coda.makeParameter({
      type: coda.ParameterType.String,
      name: "key4",
      description: "Value in Key 4",
    }),
  ],

  resultType: coda.ValueType.String,
  isAction: true,

  execute: async function ([key1, key2, key3, key4, WebhookURL], context) {
    let response = await context.fetcher.fetch({
      url: WebhookURL,
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        key1: key1,
        key2: key2,
        key3: key3,
        key4: key4,
      }),
    });
    // Return values are optional but recommended. Returning a URL or other
    return "sent";
  },
});

When trying this in the doc I used

https://mydomain.com” as the authentication account and the WebhookURL is “https://mydomain.com/wp-json/tap/v1/automator/webhook/1gkgq92de

I believe errors of the form “Couldn’t find…” are shown when the response is a 404. I’d suggest looking through your logs to see if you can get more information: