---
source_url: https://www.pubnub.com/docs/serverless/functions/functions-apis/codec-module
title: Codec Module
updated_at: 2026-05-20T11:08:53.521Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Codec Module

The `codec` module provides essential Base64, Basic Auth, and query‑string utilities in PubNub Functions. Use these helpers to transform data and authenticate calls in real‑time workflows.

## Basic Auth header (basic)

Generate a Basic Authentication header for `XHR` requests with `basic()`.

Usage: `basic(username, password)`

* `username`: Basic Auth username
* `password`: Basic Auth password

Use when your Function calls third‑party APIs that require Basic Auth.

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

## Encode Base64 (btoa)

Convert a text string to Base64 with `btoa()`.

Usage: `btoa(unencoded)`

* `unencoded`: input string to encode

Use to carry binary or mixed‑charset data over JSON.

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

## Decode Base64 (atob)

Convert a Base64 string to text with `atob()`.

Usage: `atob(encoded)`

* `encoded`: input string to decode

Use to read Base64 payloads received from external services.

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

## Encode string (URL‑safe)

Encode a string to be URL‑safe with `encodeString()`.

Usage: `encodeString(input)`

* `input`: string to URL‑encode

Use to safely place values in query parameters.

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

## Parse query string (parse)

Convert a query string to a JavaScript object with `parse()`.

Usage: `parse(queryString, defaults)`

* `queryString`: input query string
* `defaults`: default values used when keys are missing

Use to read incoming query parameters into a typed object.

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

## Stringify query string (stringify)

Create a query string from an object with `stringify()`.

Usage: `stringify(params)`

* `params`: object of key/value items to stringify

Use to build outbound URLs with query parameters.

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

:::note Functions support
If you need help with a scenario not covered here, contact [PubNub Support](https://www.pubnub.com/docs/mailto:support@pubnub.com).
:::

## Terms in this document

* **PubNub** - PubNub is a real-time messaging platform that provides APIs and SDKs for building scalable applications. It handles the complex infrastructure of real-time communication, including: Message delivery and persistence, Presence detection, Access control, Push notifications, File sharing, Serverless processing with Functions and Events & Actions, Analytics and monitoring with BizOps Workspace, AI-powered insights with Illuminate.
