BindKit

Developers

Custom components

Your own keys in every sample. Sign in and the code on this page uses your web agent's publishable key.

Sign in

Make a component in the dashboard and list its fields. The agent fills them in from the conversation and your tools, and BindKit checks them. The messenger draws the component with the code you wrote in the dashboard, or with a render your page passes to BindKit.defineComponent().

app.js
BindKit.defineComponent('order_status', {
  // html`` escapes every value, so the fields the agent fills in can't inject HTML.
  render: ({ props, action, html }) => html`
    <div class="order">
      <strong>Order ${props.number}</strong>
      <span>${props.status}</span>
      <button onclick=${() => action('track')}>Track it</button>
    </div>
  `,
  // Your page's CSS does not reach the component, so its styles go here.
  styles: `
    .order { display: grid; gap: 6px; padding: 12px; border: 1px solid #e4e4e7; border-radius: 12px; }
  `,
  onAction({ action, props, user, threadId }) {
    if (action === 'track') location.assign(`/orders/${props.number}/tracking`)
  },
})

Change what a component shows

Each component also has a state, which starts as {}. Pass one to action(id, state) and the component is drawn with it at once. Or return one from onAction, such as after calling your API; until it resolves, pending is true. If onAction throws, the component goes back to the state it had. The state is kept with the conversation, so a visitor who comes back sees it the same way, and your agent and your team see it too.

app.js
BindKit.defineComponent('order_status', {
  render: ({ props, state, pending, action, html }) =>
    state.status === 'cancelled'
      ? html`<p>Order ${props.number} is cancelled.</p>`
      : html`<button disabled=${pending} onclick=${() => action('cancel')}>
          ${pending ? 'Cancelling…' : 'Cancel the order'}
        </button>`,
  async onAction({ action, props }) {
    if (action === 'cancel') {
      const response = await fetch(`/api/orders/${props.number}/cancel`, { method: 'POST' })
      // Throwing keeps the component as it was, so it never shows a cancel that didn't happen.
      if (!response.ok) throw new Error('The order was not cancelled')
      // The component is drawn with this state from now on.
      return { status: 'cancelled' }
    }
  },
})

React

Pass your components to <BindKit /> in components, by their name in the dashboard. render gets the same props, state, pending and action, and returns JSX. Have it return a component of your own, as here: that one sits inside your app, so your hooks and context work in it, whereas a hook called in render itself breaks once a second component is drawn. Your page's CSS still does not reach it, so its styles go in styles.

App.jsx
import { BindKit } from '@bindkit/react'

function OrderStatus({ props, state, pending, action }) {
  if (state.status === 'cancelled') return <p>Order {props.number} is cancelled.</p>
  return (
    <button disabled={pending} onClick={() => action('cancel')}>
      {pending ? 'Cancelling…' : 'Cancel the order'}
    </button>
  )
}

const components = {
  order_status: {
    render: (component) => <OrderStatus {...component} />,
    async onAction({ action, props }) {
      if (action === 'cancel') {
        const response = await fetch(`/api/orders/${props.number}/cancel`, { method: 'POST' })
        // Throwing keeps the component as it was, so it never shows a cancel that didn't happen.
        if (!response.ok) throw new Error('The order was not cancelled')
        // The component is drawn with this state from now on.
        return { status: 'cancelled' }
      }
    },
  },
}

export function App() {
  return <BindKit publishableKey="pk_..." components={components} />
}

Put an agent in your product this week