> 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

Sample code for handling rates streaming in Node.js.

### 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
