---
source_url: https://www.pubnub.com/docs/serverless-sdk/modules-and-libraries/codec
title: codec
updated_at: 2026-09-30T07:20:08.000Z
---

# codec

## Documentation index

To discover more PubNub resources:

1. Fetch [PubNub's llms.txt](https://www.pubnub.com/llms-full.txt) for a list of available pages in Markdown format.
2. Identify relevant URLs from that index.
3. Fetch the target pages.

Do not assume a path exists, always check the index first.

The `codec` built-in modules are available only inside a PubNub Function. They encode and decode HTTP Basic Authentication credentials, Base64 values, and query strings.

## codec/auth

```javascript
const basicAuth = require('codec/auth');
```

### basic

`basic(username, password)`

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| username | String | Yes |  | Basic Auth username. |
| password | String | Yes |  | Basic Auth password. |

Returns: an HTTP Basic Authentication header value.

```javascript
const basicAuth = require('codec/auth');
console.log(basicAuth.basic('many', 'blocks')); // Basic: bWFueTpibG9ja3M=
```

## codec/base64

```javascript
const base64Codec = require('codec/base64');
```

| Method | Signature | Returns |
| --- | --- | --- |
| `btoa` | `btoa(unencoded)` | `unencoded` encoded as Base64. |
| `atob` | `atob(encoded)` | Base64 string `encoded` decoded to text. |
| `encodeString` | `encodeString(input)` | `input` encoded to a URL-safe string. |

```javascript
const base64Codec = require('codec/base64');
console.log(base64Codec.btoa('hello')); // aGVsbG8=
console.log(base64Codec.atob('aGVsbG8=')); // hello
console.log(base64Codec.encodeString('+')); // _
```

## codec/query_string

```javascript
const queryStringCodec = require('codec/query_string');
```

| Method | Signature | Returns |
| --- | --- | --- |
| `parse` | `parse(queryString, defaults)` | Object parsed from `queryString`, with `defaults` filled in for missing keys. |
| `stringify` | `stringify(params)` | Query string built from the `params` object. |

```javascript
const queryStringCodec = require('codec/query_string');
console.log(queryStringCodec.parse('a=5&b=10', { c: 15 })); // {a: 5, b: 10, c: 15}
console.log(queryStringCodec.stringify({ a: 10, b: 15 })); // a=10&b=15
```

## Related reference

* [Modules and libraries](https://www.pubnub.com/docs/serverless-sdk/modules-and-libraries/overview.md). Every built-in module available inside a Function.
* [crypto](https://www.pubnub.com/docs/serverless-sdk/modules-and-libraries/crypto.md). Uses `codec/base64` to prepare a key for `hmac()`.

Last updated at: 2026-09-30T07:20:08.000Z
