---
title: Formatters API
permalink: /docs/formatters-api/
---

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

-   [Usage][1]
-   [Types][2]
    -   [formatter][3]
-   [Included formatters][4]
    -   [formatText][5]
    -   [formatJSON][6]
    -   [formatJSONP][7]
    -   [formatBinary][8]

## Usage

Restify comes bundled with a selection of useful formatters that prepare your
responses for being sent over the wire, but you are free to include your own!

```js
function formatGraphQL(req, res, body) {
    var data = body;
    /* Do a thing to data */
    res.setHeader('Content-Length', Buffer.byteLength(data));
    return data;
}

var server = restify.createServer({
    formatters: {
        'application/graphql': formatGraphQL
    }
});

// Your application now supports content-type 'application/graphql'
```


## Types




### formatter

Format a response for being sent over the wire

Type: [Function][9]

**Parameters**

-   `req` **[Object][10]** the request object (not used)
-   `res` **[Object][10]** the response object
-   `body` **[Object][10]** response body to format

Returns **[String][11]** formatted response data

## Included formatters

restify comes pre-loaded with a standard set of formatters for common
use cases.


### formatText

Formats the body to 'text' by invoking a toString() on the body if it
exists. If it doesn't, then the response is a zero-length string.

**Parameters**

-   `req` **[Object][10]** the request object (not used)
-   `res` **[Object][10]** the response object
-   `body` **[Object][10]** response body. If it has a toString() method this
                              will be used to make the string representation

Returns **[String][11]** data

### formatJSON

JSON formatter. Will look for a toJson() method on the body. If one does not
exist then a JSON.stringify will be attempted.

**Parameters**

-   `req` **[Object][10]** the request object (not used)
-   `res` **[Object][10]** the response object
-   `body` **[Object][10]** response body

Returns **[String][11]** data

### formatJSONP

JSONP formatter. like JSON, but with a callback invocation.
Unicode escapes line and paragraph separators.

**Parameters**

-   `req` **[Object][10]** the request object
-   `res` **[Object][10]** the response object
-   `body` **[Object][10]** response body

Returns **[String][11]** data

### formatBinary

Binary formatter.

**Parameters**

-   `req` **[Object][10]** the request object
-   `res` **[Object][10]** the response object
-   `body` **[Object][10]** response body

Returns **[Buffer][12]** body

[1]: #usage

[2]: #types

[3]: #formatter

[4]: #included-formatters

[5]: #formattext

[6]: #formatjson

[7]: #formatjsonp

[8]: #formatbinary

[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function

[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[11]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[12]: https://nodejs.org/api/buffer.html
