MallaDocs

Build a backend endpoint

Store data in a native table and expose it with a server workflow, a webhook and a cron.

User guide
Advanced30 min

You will build: A feedback table with a public API to read and add rows, a webhook that receives feedback from another tool, a daily report and a page that lists the feedback

Read first: Tour of the editorInteractive page with workflows

In this tutorial you build a small backend without leaving the editor. You store feedback in a native table, expose it with HTTP endpoints, accept it from other tools with a webhook and send yourself a daily summary on a schedule. At the end, a page on your site lists the feedback through the endpoint.

Build a backend endpointVideo coming soon. The written steps below cover everything.
Build a backend endpoint· 10 min

Create the table

  1. Add a table

    Open the Tables workspace from the bottom of the left sidebar. Press New next to Tables, type feedback and press ↵. The table opens on its Schema tab with one text column, title.

  2. Add the columns

    Press Add column twice. Name the first column message and keep the type Text. Name the second rating and set its type to Number. Check Required on title and rating.

    Every row also gets id, createdAt and updatedAt, so you do not add them.

  3. Add a write rule

    Under Row rules, type row.rating >= 1 && row.rating <= 5 in Write. Visitors and workflows can now only store ratings from 1 to 5. Leave Read empty, so everyone can read. Press Save schema.

  4. Add a row by hand

    Switch to the Rows tab and press Add row. Fill in a title, a message and a rating, then press Add row in the panel. Edits in the editor skip the row rules, because you own the site.

Checkpoint

The feedback table shows one row in the Rows tab, and the table header shows the Rules badge.

Read rows with an endpoint

  1. Create a server workflow

    Open the Logic workspace and switch the toggle at the top from Client to Server. Press + (Add server workflow) and choose HTTP endpoint. Name the workflow List feedback.

  2. Set the trigger

    In Trigger, set Method to GET, Path to /feedback and Who can call it to Public. The hint under the fields shows the address: /api/feedback on your site.

  3. Query the table

    Press Add step and choose Query table under Tables. Type feedback in Table; workflows find a table by its name or its id. Set Limit to 20. The step card shows its id, s1.

  4. Return the rows

    Press Add step and choose Return under Server. Set Value to steps.s1.rows. The value of Return becomes the response of the endpoint.

  5. Test it

    Press Test run. The run panel streams each step with its result and duration; the Return step shows your row. Every run is kept under Runs.

Add rows with an endpoint

  1. Create a POST endpoint

    Add another HTTP endpoint workflow named Add feedback. Set Method to POST, Path to /feedback and Who can call it to Public. A GET and a POST can share a path.

  2. Insert the row

    Add an Insert row step with Table feedback and Values { title: input.title, message: input.message, rating: input.rating }. The JSON body of the request arrives as input.

  3. Return the new row

    Add a Return step with Value steps.s1, the inserted row with its id.

  4. Test with sample input

    In Test input, type { "title": "Fast support", "message": "Answered in an hour", "rating": 5 } and press Test run. Then change the rating to 9 and run again: the write rule refuses the row and the run fails.

Checkpoint

The Rows tab of the feedback table shows the row from the test run, and the run history shows one succeeded run and one failed run.

Receive a webhook

  1. Create the webhook

    Add a server workflow and choose Webhook. Name it Feedback webhook and set Path to /webhooks/feedback. A webhook always answers POST.

  2. Store the payload

    Add an Insert row step with Table feedback and the same Values as before. The payload of the webhook arrives as input.

Send a daily report

  1. Create a schedule

    Add a server workflow and choose Schedule. Name it Daily report and press the Every day at 09:00 preset. Schedules use UTC, and the hint under the field reads the schedule back to you.

  2. Find the low ratings

    Add a Query table step with Table feedback and Filter [{ column: 'rating', op: 'lte', value: 2 }].

  3. Email yourself

    Add a Send email step. Set To to 'you@example.com', Subject to Daily feedback and Text to the expression steps.s1.rows.length + ' ratings of 2 or less'. Press Test run to get the email now.

Publish and call the endpoint

  1. Publish to staging

    Server workflows save as you edit, but they go live only when you publish. Press Publish in the toolbar, keep Staging selected and confirm. From now on the schedule runs every day on staging.

  2. Call it from a terminal

    Run curl https://<your-staging-address>/api/feedback. The answer is JSON with the run id, its status and the rows in result.

Show the feedback on a page

  1. Connect your site's API

    Open the Data workspace, press + (Add integration) next to Integrations and choose REST API. Set the base URL to your staging address and press Create integration.

  2. Create a query

    Press + (Add query) next to Queries and name it feedback. Pick your integration and the Request operation. In Input, set the path to /api/feedback. Set Response path to result, keep When it runs on Client and press Run.

  3. List the rows

    Insert a Collection list on the page. In the Collection list section, set Source to Expression and Items to queries.feedback.data. Inside the item, bind a text to item.title and another to item.message.

Done

The page lists the feedback from your table, read through your own endpoint. New rows from the POST endpoint or the webhook show up on the next load.

What you learned

  • A native table has a schema, a data browser and row rules that apply to visitors and workflows.
  • A server workflow starts from an HTTP endpoint, a webhook or a schedule, and Return sets the response.
  • Test run runs the draft with sample input and shows every step; publishing puts the workflows live.

Keep going with the next tutorials, or read the guides behind this one: