Skip to main content
The connection function creates a connection definition for use in custom components and code-native integrations. Connections bundle a set of credential fields (such as an API key or base URL) that integration builders configure once and reference across multiple action steps.

Function signature

function connection<T extends DefaultConnectionDefinition>(definition: T): T

Parameters

definition
DefaultConnectionDefinition
required
An object describing the connection. See fields below.

DefaultConnectionDefinition fields

definition.key
string
required
A unique programmatic identifier for this connection type within the component.
definition.display
ConnectionDisplayDefinition
required
Controls how the connection appears in the Prismatic UI.
definition.inputs
Record<string, ConnectionInput>
required
A map of input field definitions that the integration builder fills in to configure this connection. Uses ConnectionInput — a variant of the standard input field that omits the clean function and adds shown and writeOnly options.

Return type

Returns the same DefaultConnectionDefinition object passed in, after validation. Pass the returned value to the connections array in component.

Example

import { connection, component } from "@prismatic-io/spectral";

export const apiKeyConnection = connection({
  key: "apiKey",
  display: {
    label: "API Key",
    description: "Authenticate using an API key",
  },
  inputs: {
    endpoint: {
      label: "API Endpoint",
      type: "string",
      required: true,
      default: "https://api.example.com",
      placeholder: "https://api.example.com",
      comments: "The base URL of the API",
      example: "https://api.acme.com",
    },
    apiKey: {
      label: "API Key",
      type: "password",
      required: true,
      shown: true,
      writeOnly: true,
      placeholder: "Your API key",
    },
    region: {
      label: "Region",
      type: "string",
      required: false,
      model: [
        { label: "US East", value: "us-east" },
        { label: "EU West", value: "eu-west" },
      ],
      default: "us-east",
    },
  },
});

export default component({
  key: "myComponent",
  display: {
    label: "My Component",
    description: "Example component",
    iconPath: "icon.png",
  },
  connections: [apiKeyConnection],
});

Using a connection in an action

import { action, input } from "@prismatic-io/spectral";

export const fetchData = action({
  display: { label: "Fetch Data", description: "Get data from the API" },
  inputs: {
    connection: input({ label: "Connection", type: "connection", required: true }),
  },
  perform: async (context, params) => {
    const { endpoint, apiKey } = params.connection.fields as {
      endpoint: string;
      apiKey: string;
    };

    const response = await fetch(`${endpoint}/data`, {
      headers: { Authorization: `Bearer ${apiKey}` },
    });

    return { data: await response.json() };
  },
});