Skip to content

Custom responses

nsmirn0v edited this page Feb 11, 2023 · 1 revision

By default mockapi.io returns a single item or a collection of items depending on the API call. Assuming you have tasks resource created:

Endpoint Response
GET /tasks Task[]
GET /tasks/{taskId} Task
POST /tasks Task
PUT /tasks/{taskId} Task
Patch /tasks/{taskId} Task
Delete /tasks/{taskId} Task

Sometimes we want to customize the data. For example, to implement pagination we might need the response to look something like this:

{
  "count": 60,
  "tasks": [
    {
      "completed": false,
      "title": "Checkout mockapi.io!",
      "id": "1"
     }
  ]
}

Fortunately, mockapi.io provides a simple interface to do just that. Open the Resource modal and scroll down to Endpoints section. Each endpoint has a field that defines its response. The default value is $mockData which refers to the actual record (or collection of records) stored in the database.

Screen Shot 2023-02-10 at 5 18 13 PM

You can replace it with desired JSON, for example:

{
  "anyKey": "anyValue",
  "count": "$count",
  "tasks": "$mockData",
  "requestId": "$datatype.uuid"
}
  • "anyKey": "anyValue" is static data and will always be the same for all requests
  • $count will be replaced with the number of records in the database
  • $mockData will be replaced with actual record (or collection of records)
  • $datatype.uuid is a Faker.js method and will be executed on every API call

Available Faker.js methods:

Method
address.buildingNumber
address.cardinalDirection
address.city
address.cityName
address.country
address.countryCode
address.county
address.direction
address.latitude
address.longitude
address.nearbyGPSCoordinate
address.ordinalDirection
address.secondaryAddress
address.state
address.stateAbbr
address.streetAddress
address.streetName
address.streetPrefix
address.streetSuffix
address.timeZone
address.zipCode
address.zipCodeByState
animal.bear
animal.cat
animal.cetacean
animal.cow
animal.crocodilia
animal.dog
animal.fish
animal.horse
animal.insect
animal.lion
animal.rabbit
animal.rodent
animal.snake
animal.type
color.cmyk
color.hsl
color.human
color.hwb
color.lab
color.lch
color.rgb
commerce.color
commerce.department
commerce.price
commerce.product
commerce.productAdjective
commerce.productDescription
commerce.productMaterial
commerce.productName
company.bs
company.bsAdjective
company.bsBuzz
company.bsNoun
company.catchPhrase
company.catchPhraseAdjective
company.catchPhraseDescriptor
company.catchPhraseNoun
company.companyName
company.companySuffix
company.suffixes
database.collation
database.column
database.engine
database.mongodbObjectId
database.type
datatype.array
datatype.boolean
datatype.datetime
datatype.float
datatype.hexadecimal
datatype.json
datatype.number
datatype.string
datatype.uuid
date.between
date.betweens
date.birthdate
date.future
date.month
date.past
date.recent
date.soon
date.weekday
finance.account
finance.accountName
finance.amount
finance.bic
finance.bitcoinAddress
finance.creditCardCVV
finance.creditCardNumber
finance.currencyCode
finance.currencyName
finance.currencySymbol
finance.ethereumAddress
finance.iban
finance.litecoinAddress
finance.mask
finance.pin
finance.routingNumber
finance.transactionDescription
finance.transactionType
git.branch
git.commitEntry
git.commitMessage
git.commitSha
git.shortSha
hacker.abbreviation
hacker.adjective
hacker.ingverb
hacker.noun
hacker.phrase
hacker.verb
image.abstract
image.animals
image.avatar
image.business
image.cats
image.city
image.dataUri
image.fashion
image.food
image.image
image.imageUrl
image.nature
image.nightlife
image.people
image.sports
image.technics
image.transport
internet.avatar
internet.color
internet.domainName
internet.domainSuffix
internet.domainWord
internet.email
internet.emoji
internet.exampleEmail
internet.httpMethod
internet.httpStatusCode
internet.ip
internet.ipv4
internet.ipv6
internet.mac
internet.password
internet.port
internet.protocol
internet.url
internet.userAgent
internet.userName
lorem.lines
lorem.paragraph
lorem.paragraphs
lorem.sentence
lorem.sentences
lorem.slug
lorem.text
lorem.word
lorem.words
mersenne.rand
music.genre
music.songName
name.findName
name.firstName
name.gender
name.jobArea
name.jobDescriptor
name.jobTitle
name.jobType
name.lastName
name.middleName
name.prefix
name.sex
name.sexType
name.suffix
phone.imei
phone.phoneFormats
phone.phoneNumber
phone.phoneNumberFormat
random.alpha
random.alphaNumeric
random.locale
random.numeric
random.word
random.words
science.chemicalElement
science.unit
system.commonFileExt
system.commonFileName
system.commonFileType
system.cron
system.directoryPath
system.fileExt
system.fileName
system.filePath
system.fileType
system.mimeType
system.networkInterface
system.semver
vehicle.bicycle
vehicle.color
vehicle.fuel
vehicle.manufacturer
vehicle.model
vehicle.type
vehicle.vehicle
vehicle.vin
vehicle.vrm
word.adjective
word.adverb
word.conjunction
word.interjection
word.noun
word.preposition
word.verb
Clone this wiki locally