# JSON API for Limit Control

**URL:** <https://discuss.jsonapi.org/t/json-api-for-limit-control/568>\
**Category:** Uncategorized\
**Created:** [June 20, 2016, 3:07pm UTC](https://discuss.jsonapi.org/t/json-api-for-limit-control/568 "2016-06-20T15:07:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgabrielygalan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/jgabrielygalan/32/293_2.png) [@jgabrielygalan](https://discuss.jsonapi.org/u/jgabrielygalan)\
**Post date:** [June 20, 2016, 3:07pm UTC](https://discuss.jsonapi.org/t/json-api-for-limit-control/568/1 "2016-06-20T15:07:17Z")

</div>

Hi,  
We are looking to use Json API for an internal service that we need. It’s a Expense limit control. Our services include charging subscribed users periodically certain amounts, but we have the possibility to cap the chargings if they go above configurable limits (for example, monthly, by service, etc). For this we plan to have a running count of expenses by user and some other dimensions: day, month, service, periods, etc. We have some problems wrapping our heads around what would be a correct representation of this for Json API, and specifically how to handle the operation of atomically checking if a new expense would pass any limit to stop it, or add to the current counts if it doesn’t. The actual list of individual expenses doesn’t need to be part of the resource (we are not even storing them in this DB), only the running totals. We have a customer resource that represents a customer, and we were thinking to add a relationship to a resource that represents the running totals. But we are not sure how we would make a call that would allow us to check if a new amount passes the limits, and that updates the running totals. We were toying with the idea of using PATCH passing an amount, this would update the running totals or return a 409 if a limit would be passed.  
Any idea?

---

<div class="post-metadata">

**Author:** ![jlangley](https://avatars.discourse-cdn.com/v4/letter/j/9e8a1a/32.png) [@jlangley](https://discuss.jsonapi.org/u/jlangley)\
**Post date:** [June 20, 2016, 9:36pm UTC](https://discuss.jsonapi.org/t/json-api-for-limit-control/568/2 "2016-06-20T21:36:43Z")

</div>

> [@jgabrielygalan](#):
>
> We have a customer resource that represents a customer, and we were thinking to add a relationship to a resource that represents the running totals.

Or you could make the running total an `attribute` of the customer.

> [@jgabrielygalan](#):
>
> We were toying with the idea of using PATCH passing an amount, this would update the running totals or return a 409 if a limit would be passed.

You could do, or I can see a case for using `402 - payment required` or `403 - forbidden`.  
I could also see a case for using a POST to submit a claim to a collection of claims, with the total tracked in the `meta` section; but this might be less appropriate since you’re not interested in the individual claims here.  
Another option could be to use a POST to create a claim _request_ (i.e. not the actual claim).

---

<div class="post-metadata">

**Author:** ![jgabrielygalan](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/jgabrielygalan/32/293_2.png) [@jgabrielygalan](https://discuss.jsonapi.org/u/jgabrielygalan)\
**Post date:** [June 21, 2016, 9:29am UTC](https://discuss.jsonapi.org/t/json-api-for-limit-control/568/3 "2016-06-21T09:29:12Z")

</div>

> [@jlangley](#):
>
> Or you could make the running total an attribute of the customer.

A problem that we wanted to avoid is to have to always fill that attribute. There are other use cases that don’t need it, and there’s a cost to retrieve them that we would like to avoid. Is it possible to have a “phantom” attribute that is only returned if explicitly requested?

Example if requested:

```
{
  "data": {
    "type": "customer",
    "id": "234",
    "attributes": {
        "name": "test",
        "expenses": {
            "monthly": 4.99,
            "daily": 1.99
        }
    },
    "links": {
      "self": "http://localhost:8081/customer/234"
    }
  }
}

```

If not requested, the expenses attribute would not be present.

What would be the correct call to check and update? Would a PATCH to “self” be ok? What is the consensus regarding the Content-type for PATCH? We’ve been reading several topics on that but there are quite different opinions.

PATCH /customer/234  
Content-type: application/vnd.custom…

{“update”:“expenses”, “value”:2.00}

would return a 409 if 2.00 would exceed any of the limits, or 200 (and daily and monthly totals would be updated) if everything is fine.

Sounds good?

---

<div class="post-metadata">

**Author:** ![rafde](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@rafde](https://discuss.jsonapi.org/u/rafde)\
**Post date:** [July 23, 2016, 1:24am UTC](https://discuss.jsonapi.org/t/json-api-for-limit-control/568/4 "2016-07-23T01:24:58Z")

</div>

You could use meta for your phantom attribute  
GET `http://localhost:8081/customer/234?meta[expense]`

```auto
{
  "meta":{
    "expenses": {
       "monthly": 4.99,
       "daily": 1.99
     }
  },
  "data": {
    "type": "customer",
    "id": "234",
    "attributes": {
        "name": "test"
    },
    "links": {
      "self": "http://localhost:8081/customer/234"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![jlangley](https://avatars.discourse-cdn.com/v4/letter/j/9e8a1a/32.png) [@jlangley](https://discuss.jsonapi.org/u/jlangley)\
**Post date:** [July 23, 2016, 10:31pm UTC](https://discuss.jsonapi.org/t/json-api-for-limit-control/568/5 "2016-07-23T22:31:19Z")

</div>

> [@jgabrielygalan](#):
>
> What is the consensus regarding the Content-type for PATCH? We’ve been reading several topics on that but there are quite different opinions.
> 
> PATCH /customer/234Content-type: application/vnd.custom…

I believe the [spec](http://jsonapi.org/format/#content-negotiation) requires the content type to be JSON API, and no other types are allowed.
