Product updates

Liveblocks 1.8: Typed REST APIs and easy Comments notifications

Today we’re introducing new typed REST API methods, making integrating Liveblocks into your back end easier than ever. We’re also adding functions to help you transform comments, and create email notifications more easily.

Picture of Chris Nicholas
Chris Nicholas
@ctnicholasdev
Liveblocks 1.8: Typed REST APIs and easy Comments notifications

With Liveblocks 1.8, we’re introducing an improved way to consume our REST APIs with types, making it quicker to integrate Liveblocks into the back end of your application. We’re also adding a way to transform comments into HTML, Markdown, and more, meaning sending formatted email notifications is now simpler.

Upgrade now

Start using the new features now by updating each Liveblocks package in your project to the @latest version.

$npm install @liveblocks/client@latest @liveblocks/react@latest @liveblocks/react-comments@latest liveblocks/node@latest

If you use any other Liveblocks packages, make sure to include those too.

Typed REST APIs

It’s now possible to access our REST APIs using the @liveblocks/node package. Previously, the only way to access our REST APIs in Node.js was via a fetch call.

// ❌ Before, using fetchconst response = await fetch("https://api.liveblocks.io/v2/rooms", {  method: "POST",  headers: {    Authorization: "Bearer {{SECRET_KEY}}",  },  body: JSON.stringify({    id: "my-room-name",    defaultAccesses: ["room:write"],  }),});
// ❌ No typesconst room = await response.json();

From now, you can set up a Liveblocks client, call a corresponding method, and get type hints on the returned value.

// ✅ After, using `Liveblocks` and typed methodsconst liveblocks = new Liveblocks({  secret: "sk_prod_xxxxxxxxxxxxxxxxxxxxxxxx",});
// ✅ Fully-typed roomconst room = await liveblocks.createRoom("my-room-name", { defaultAccesses: ["room:write"],});

Every existing REST API is supported, and we’ll continue to add them in the future. Here’s an example of some more methods.

// Get users currently in a roomconst activeUsers = await liveblocks.getActiveUsers("my-room-id");
// Get Storage data as JSONconst storage = await liveblocks.getStorageDocument("my-room-id", "json");
// Update Yjs dataawait liveblocks.sendYjsBinaryUpdate("my-room-id", { update: Y.encodeStateAsUpdate(yDoc),});
// Get the last 20 rooms that contain your custom metadataconst canvasRooms = await liveblocks.getRooms({ limit: 20, metadata: { myRoomType: "canvas" },});

Find all the methods in the API reference under @liveblocks/node.

Transforming comments

Using Liveblocks 1.8, you can transform a comment’s text into different formats, for example HTML, Markdown, or a plain string of text. Here’s an example of a comment converted into two different formats.

Comment with example body: 'Thank you so much @Emil Joyce!', with 'so much' in bold
Thank you **so much** @Emil Joyce!
<p>Thank you <b>so much</b> <span data-mention>@Emil Joyce</span>!</p>

To enable this, we’ve added a new helper function called stringifyCommentBody. Here’s one way it could be used, alongside Liveblocks.getComment.

// Retrieve a commentconst comment = await liveblocks.getComment({  roomId: "my-room-id",  threadId: "my-thread-id",  commentId: "my-comment-id",});
// Convert comment body into Markdownconst markdownBody = await stringifyCommentBody(comment.body, { format: "markdown",});
// "Thank you **so much** emil.joyce@example.com!"console.log(markdownBody);

You can also customize the output format, either completely, or on an element-by-element basis. Make sure to read stringifyCommentBody to learn more.

Get mentioned IDs

As a bonus, we’ve also added a utility that makes it easy to find each user mentioned in a comment’s body. It’s called getMentionedIdsFromCommentBody, and here’s how it works.

Comment with example body: 'Could you take a look at this @Jody Hekla, @Tatum Paolo?'
// Retrieve a commentconst comment = await liveblocks.getComment({  roomId: "my-room-id",  threadId: "my-thread-id",  commentId: "my-comment-id",});
// Get IDs for each user mentionedconst mentionedIds = getMentionedIdsFromCommentBody(comment.body);
// ["jody.hekla@example.com", "tatum.paolo@example.com"]console.log(mentionedIds);

Sending Comments email notifications

Using the new REST API and comment transformation functions, it’s easier than ever to send formatted email notifications. You can trigger events when comments are created using our webhooks, and in particular the CommentCreatedEvent.

Example comment email notification

Here’s an example of a Next.js route handler that listens for new comments, then sends a formatted HTML email with Resend.

To learn exactly how this works, we’ve written a full guide on sending email notifications when comments are created.

Contributors

Huge thanks to everyone who contributed and specifically to Nimesh Nayaju and Marc Bouchenoire for their work on the REST API methods and the Comments functions respectively. Keep checking out the changelog for the full release notes–see you next time!