Build your own widget
A custom support panel on the messaging API.
A custom panel needs three pieces: your UI, a server proxy, and a webhook receiver.
MEDIAN_KEY stays on your server. Your panel calls your own routes and your
server relays to Median. For replies, either push webhook events to the browser
over a websocket or server-sent events, or poll your thread route while the
panel is open.
Proxy the API
Two routes are enough:
const MEDIAN = "https://api.median.sh/v1";
const headers = {
Authorization: `Bearer ${process.env.MEDIAN_KEY}`,
"Content-Type": "application/json",
};
app.get("/support/thread", async (req, res) => {
const response = await fetch(
`${MEDIAN}/thread?session=${encodeURIComponent(sessionFor(req))}`,
{ headers },
);
res.status(response.status).json(await response.json());
});
app.post("/support/messages", async (req, res) => {
const response = await fetch(`${MEDIAN}/messages`, {
method: "POST",
headers,
body: JSON.stringify({
session: sessionFor(req),
body: req.body.text,
user: { name: req.user?.name, email: req.user?.email },
}),
});
res.status(response.status).json(await response.json());
});
Render the thread
Everything the panel shows is in the thread response:
- Group consecutive
sender: "agent"messages, and draw a divider whereconversationIdchanges. - A
pendingmessage is a reply in progress. Show a typing indicator; it fills in place with the same id. - While
awaitingHumanis true, tell the visitor a person is coming. - After
statusbecomesresolved, the next message starts a new conversation automatically.
Draw buttons
Send buttons: true with every POST /v1/messages once your panel draws
them. The agent can then ask with answers to tap and offer pages. Its
messages carry them as fields, and body holds only the words:
| Field | Draw it as |
|---|---|
choices |
One button per option under the message, while it is the newest one in an open thread. A tap sends the label as the visitor’s next message. With multiple: true, let them pick several and send them joined with , |
links |
Up to 3 buttons that open url. A path like /settings/billing is a page on your own site |
icon on either is a glyph name such as card, user or book, or null.
Draw the ones you have and leave the rest plain. Without buttons, the agent
asks in words. Open a path against your own site’s origin.
Fetch the agent’s name and avatar from
/v1/config instead of hardcoding them,
so renaming the agent in the dashboard carries through.
Ask for an email
Send emailAsk: true with every POST /v1/messages once your panel can ask
for an email. When a tool needs the visitor’s address, the agent says why in
one line and the thread carries emailAsk: { at } until they answer. Webhook
events carry it on data.conversation. Without emailAsk, the agent asks in
words.
While it is set, show an email field and send the answer to
POST /v1/email-ask:
app.post("/support/email-ask", async (req, res) => {
const response = await fetch(`${MEDIAN}/email-ask`, {
method: "POST",
headers,
body: JSON.stringify({
session: sessionFor(req),
conversationId: req.body.conversationId,
// null for "Not now"
email: req.body.email,
}),
});
res.status(response.status).json(await response.json());
});
| Response | Meaning |
|---|---|
200 |
Saved on the visitor. The agent carries on with it, or without it for null |
400 invalid_email |
Show the message under the field. The ask stays open |
409 no_open_ask |
Already answered, or the thread moved on. Read the thread again |
Receive replies
Subscribe your receiver to message.created,
verify the signature, and forward
data.message to the right visitor. Keep a map from conversationId to
session. Update it from every POST /v1/messages response, which returns
{ conversationId, messageId }. The id changes when a resolved conversation
is followed by a new message.
Filter what arrives. message.created also fires for the visitor’s own
messages (sender: "visitor"), which your panel already shows, and for
conversations from every other channel. Skip visitor messages and any
conversationId your map does not hold.
What you give up
| Feature | Status |
|---|---|
| Signed identity | Widget only. POST /v1/messages takes no identity, so in an API conversation your tools see verified: false and no externalId. A tool that needs a signed in account refuses, and the agent never asks the visitor to sign in |
| Realtime without your own server | Widget only. It ships its own connection |