> For the complete documentation index, see [llms.txt](https://docs.terminal.africa/tship/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.terminal.africa/tship/miscallaneous/rates-stream-handling.md).

# Rates Stream Handling

### Handling Rate Stream Events (Node.js)

```js
const action = async () => {
    try {
        let shipment_api_url = `https://api.terminal.africa/v1/rates/shipment/stream?shipment_id=SH-ZQKAPYJXGYN4GDES`; 
        let options = {
            headers: {
                Authorization: "Bearer {SECRET_KEY}"
            }
        }
        const res = await fetch(shipment_api_url, options);
        
        if (!res.ok) {
            let res_json = await res.json();
            DEBUG("Response JSON - %O", res_json);
            let message = res_json.message || `Rate stream failed: ${res.status} ${res.statusText}`;
            throw new Error(message);
        }

        if (!res.body) {
            throw new Error("Response body is empty");
        }

        const reader = res.body.getReader();
        const dec = new TextDecoder();
        let buf = "";

        for (;;) {
            const { value, done } = await reader.read();
            DEBUG("Done - %O", done);

            if (done) {
                buf += dec.decode(); // flush decoder
                break;
            }

            buf += dec.decode(value, { stream: true });

            const lines = buf.split("\n");
            buf = lines.pop() ?? "";

            for (const line of lines) {
                if (!line.trim()) continue;

                try {
                    const evt = JSON.parse(line);

                    if (evt.type === "carrier") {
                        // merge evt.rates
                    } else if (evt.type === "complete") {
                        // final evt.data
                        // optional: await reader.cancel(); break;
                    } else if (evt.type === "error") {
                        // handle backend stream error event if supported
                    }
                } catch (err) {
                    console.log(err)
                }
            }
        }
    }catch(error) {
        console.log(error);
    }
}

action();
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.terminal.africa/tship/miscallaneous/rates-stream-handling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
