Blog App: Create a Web Client

Deployment Type:

Author: Stitch Documentation Team

In this guide, we’ll create the frontend of our blog. Our blog frontend needs to:

  • allow users to log in
  • allow users to create comments on a blog post
  • allow users to view the comments of other users on blog posts

Since our backend is already set up, all we need to do is call the right APIs for each of these actions.

Time required: 15 minutes

What You’ll Need

If you have not yet built the backend for the blog, follow the steps in the Create the Backend guide.



You can download the completed blog.html file for this tutorial in the blog folder in the tutorials repo.


Create an HTML page

Begin by creating a file named blog.html and copy the following HTML into the file:

    <h3>This is a great blog post</h3>
    <div id="content">
      I like to write about technology because I want to get on the
      front page of hacker news.
    <div id="comments"></div>

Include the MongoDB Stitch JavaScript SDK

We need the JavaScript SDK to communicate with Stitch from our blog code. To include the SDK, add the following <script> tag to the <head> section of blog.html:

<script src=""></script>

Initialize Stitch Clients

Apps communicate with Stitch by calling methods on client objects from the Stitch SDK. We need to initialize an app client and a MongoDB Service client to store comments in MongoDB.

To set up Stitch:

  1. Paste the following <script> tag in blog.html beneath the tag that imports the Stitch SDK:

      // Initialize the App Client
      const client = stitch.Stitch.initializeDefaultAppClient("<your-app-id>");
      // Get a MongoDB Service Client
      const mongodb = client.getServiceClient(
      // Get a reference to the blog database
      const db = mongodb.db("blog");
  2. In the code you just pasted, replace <your-app-id> with your Stitch application’s App ID. You can find the App ID on the Clients page of the Stitch UI.


Query and Display Comments on Page Load

We can use the MongoDB Service client we just created to query the blog.comments collection. Once we have the comments documents, we can map them to HTML and display them on the blog post.

To query and display comments, add the following function to the <script> tag:

function displayComments() {
    .find({}, {limit: 1000})
    .then(docs => {
      const html = => `<div>${doc.comment}</div>`);
      document.getElementById("comments").innerHTML = html;


MongoDB Stitch requires users to log in before interacting with any Stitch service. Even though we configured the rules for the blog.comments collection, this query won’t work unless a user is logged in.


Log In and Display Comments On Load

Users log in to Stitch applications with a provider-specific credential object that we pass to the loginWithCredential() function. We created an anonymous authentication provider in Stitch, so we need users to log in with an anonymous credential.

To set up anonymous authentication in blog.html:

  1. Add the following function to the <script> tag:

    function displayCommentsOnLoad() {
        .loginWithCredential(new stitch.AnonymousCredential())
  2. Update the <body> tag in blog.html to display comments when the page loads.

    <body onload="displayCommentsOnLoad()">

Add a Form for Submitting Comments

We now have everything we need to query and display comments that are already in MongoDB. All that’s left to do is to wire up a form that lets users insert new comments.

To let users add new comments:

  1. Add the addComment() function to the <script> tag:

    function addComment() {
      const newComment = document.getElementById("new_comment");
      console.log("add comment",
        .insertOne({ owner_id :, comment: newComment.value })
      newComment.value = "";
  2. Add a comment form by pasting the following code at the bottom of the <body> tag in blog.html:

    Add comment:
    <input id="new_comment"><input type="submit" onClick="addComment()">


Congratulations, you now have a working blog! Try refreshing the page and submitting a comment on the blog post.

What’s Next

Check out some of our suggestions to improve your blog even more!