# Representing price in response and request

**URL:** https://discuss.jsonapi.org/t/representing-price-in-response-and-request/1423
**Category:** Meta
**Created:** [November 16, 2018, 12:28pm UTC](https://discuss.jsonapi.org/t/representing-price-in-response-and-request/1423 "2018-11-16T12:28:57Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![lucas03](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/lucas03/32/574_2.png) [@lucas03](https://discuss.jsonapi.org/u/lucas03)
#### Post date: [November 16, 2018, 12:28pm UTC](https://discuss.jsonapi.org/t/representing-price-in-response-and-request/1423/1 "2018-11-16T12:28:57Z")

</div>

How are you representing price amount in your APIs? I think most popular choices are string and number (in Python, unfortunately, it is converted to float by default - connexion framework). `type: number, format: decimal` is not supported yet I believe. What is the best practice for this?

In case of number, I guess there could be added some converter to convert number to decimal right after validation?

Strings I think are easier for handling, would be converted to decimal inside handler. But then there is missing validation -\> price is a number, not a string obviously. (solved by custom validation?)

I’ve seen this related issue in [OpenAPI](https://github.com/OAI/OpenAPI-Specification/issues/316#issuecomment-86594998). Does json:api have a suggestion here?

---

<div class="post-metadata">

### Author: ![lucas03](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/lucas03/32/574_2.png) [@lucas03](https://discuss.jsonapi.org/u/lucas03)
#### Post date: [November 16, 2018, 1:55pm UTC](https://discuss.jsonapi.org/t/representing-price-in-response-and-request/1423/2 "2018-11-16T13:55:03Z")

</div>

Fun fact: Int was actually suggestion by Mark Ralphson, member of OpenAPI initiative.

_Relevant texts to price in API_

> <https://stackoverflow.com/questions/35709595/why-would-you-use-a-string-in-json-to-represent-a-decimal-number>

  
[https://allegro-restapi-guideline.readthedocs.io/en/latest/Representation/#price-and-currency](https://allegro-restapi-guideline.readthedocs.io/en/latest/Representation/#price-and-currency)  

> **[How a RESTful API represents resources](https://www.oreilly.com/ideas/how-a-restful-api-represents-resources)**
>
> Formats, linking, and versioning are important in well-formed RESTful APIs.

_Usages:_

_String_  
[https://developer.paypal.com/docs/api/overview/#make-your-first-call](https://developer.paypal.com/docs/api/overview/#make-your-first-call)  
[http://developers.payu.com/en/restapi.html](http://developers.payu.com/en/restapi.html)

> **[Payment](https://help.shopify.com/en/api/reference/sales-channels/payment)**
>
> Create and update payments to provide a custom checkout experience for your sales channel.

  
[https://w3c.github.io/payment-request/](https://w3c.github.io/payment-request/)  

> **[Twilio Pricing REST API - Phone Numbers Resource](https://www.twilio.com/docs/phone-numbers/pricing)**
>
> Twilio's Phone Numbers resource allows users of the Pricing API to view pricing information for Twilio phone numbers in different countries and area codes.

  

> **[NationalBankBelgium/REST-API-Design-Guide](https://github.com/NationalBankBelgium/REST-API-Design-Guide/wiki/REST-Resources-Actions)**
>
> NBB's REST-ish API Design Guide. Contribute to NationalBankBelgium/REST-API-Design-Guide development by creating an account on GitHub.

_Number_

> **[API design guidance - Best practices for cloud applications](https://docs.microsoft.com/en-us/azure/architecture/best-practices/api-design)**
>
> Guidance on how to create a well-designed web API.

  
there are more of course, but we should not decide based on which one is used the most.

I’d use `{"type": "string", "format": "decimal"}` with custom validation.

---

<div class="post-metadata">

### Author: ![a-komarev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/a-komarev/32/440_2.png) [@a-komarev](https://discuss.jsonapi.org/u/a-komarev)
#### Post date: [November 21, 2018, 7:40pm UTC](https://discuss.jsonapi.org/t/representing-price-in-response-and-request/1423/3 "2018-11-21T19:40:49Z")

</div>

Hi, @lucas03!

In my projects I’m storing money amounts in cents, so there is no need to use decimal.

```auto
type: "Service",
attributes: {
    name: "Design an API",
    price: {
        currencyCode: "USD",
        amount: 12345678
    }
}

```

I didn’t clearly understood why you want to declare `"type": "string"`. If you want to have money amount in your relationships - then you should create own type of resource. Call it as you want and how it’s better describe your resource: `Price`, `Money` or whatever.

```auto
relationships: {
    price: {
        data: {
            type: "Price",
            id: "price-1"
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![lucas03](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/lucas03/32/574_2.png) [@lucas03](https://discuss.jsonapi.org/u/lucas03)
#### Post date: [November 21, 2018, 8:01pm UTC](https://discuss.jsonapi.org/t/representing-price-in-response-and-request/1423/4 "2018-11-21T20:01:43Z")

</div>

Hi and thanks for answer 🙂 I don’t wanna use strings, as I need to support multiple currencies with various precision.

I’ll create custom type, but I wanna avoid having it as number. As people often cast it to float, which is obviously bad for representing money. More on that in first three links I posted.

---

<div class="post-metadata">

### Author: ![a-komarev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/a-komarev/32/440_2.png) [@a-komarev](https://discuss.jsonapi.org/u/a-komarev)
#### Post date: [November 21, 2018, 8:05pm UTC](https://discuss.jsonapi.org/t/representing-price-in-response-and-request/1423/5 "2018-11-21T20:05:52Z")

</div>

Each currency has own precision, but any currency has their own “cents”, so you can store amount in it. The only problem with it: frontend application need to fetch collection of all supported currencies and get this precision from it, to know on what number they should divide money amount to receive dollars, euros, etc instead of cents, eurocents, etc.
