REST API

Overview

Endpoints are automatically created based on the models in your config.

This is best explained with an example. Let's consider Mockend's own .mockend.json (opens in a new tab) used in our demo. It contains Post and Comment models with some fields and relations.

Note that model names must be CamelCased and singular.

mockend.yml
models:
  Post:
    hasMany: [Comment]
    fake:
      _count: 10
      cover: { imageURL: [1920, 1080] }
      title: { loremWords: [3, 10] }
      body: loremParagraphs
      category: { stringOf: [one, two, three] }
      isDraft: { bool: 90 }
      views: { int: [0, 1000] }
      createdAt: dateTime
 
  Comment:
    belongsTo: [Post]
    fake:
      _count: 25
      email: email
      body: loremParagraphs
      createdAt: { dateTime: [2010-01-01T00:00:00Z, 2020-12-31T23:59:59Z] }

Endpoints

Mockend will automatically create Post and Comment endpoints.

GET    .../posts
GET    .../posts/:id
POST   .../posts
PUT    .../posts/:id
PATCH  .../posts/:id
DELETE .../posts/:id
GET    .../comments
GET    .../comments/:id
POST   .../comments
PUT    .../comments/:id
PATCH  .../comments/:id
DELETE .../comments/:id

Note that POST/PUT/PATCH/DELETE are completely mocked endpoints. You or your users can freely call these endpoints without having to worry about data getting dirty.

Query

Resources can be queried, filtered and sorted using various query parameters.

Operators

ParamDescription
<field>_eq==
<field>_ne!=
<field>_gt>
<field>_gte>=
<field>_lt<
<field>_lte<=

Search

ParamDescription
<field>_containsfield contains string
<field>_startsWithfield starts with string
<field>_endsWithfield ends with string

Sort

ParamDescription
<field>_order=asc|descsort array

Paginate

ParamDescription
_limitnumber of items in array
_offsetstart of array

Example requests

Here are some working examples to get you started.

URLDescription
.../posts/1 (opens in a new tab)Post with id 1
.../posts?createdAt_order=desc (opens in a new tab)Posts sorted by createdAt
.../posts?category_eq=one (opens in a new tab)Posts in category one
.../posts?title_contains=hello (opens in a new tab)Posts with title containing hello
.../posts?_limit=5&views_order=desc (opens in a new tab)Top 5 posts sorted by views
.../posts?isDraft_eq=false&views_lt=100 (opens in a new tab)Published posts with less than 100 views
.../posts?_offset=20&_limit=10 (opens in a new tab)Page 2 with 10 results per page

Nested resources

See relations.