> For the complete documentation index, see [llms.txt](https://docs.hostedgraphite.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hostedgraphite.com/language-guide/typescript.md).

# TypeScript

### Sending a metric via TCP

```typescript
import * as net from 'net';

const socket = net.createConnection({ host: 'carbon.hostedgraphite.com', port: 2003 }, () => {
    socket.write('YOUR-API-KEY.foo.ts-tcp 1.2\n');
    socket.end();
});
```

### Sending a metric via UDP

```typescript
import * as dgram from 'dgram';

const socket = dgram.createSocket('udp4');

socket.send('YOUR-API-KEY.foo.ts-udp 1.2\n', 2003, 'carbon.hostedgraphite.com', () => {
    socket.close();
});
```

### Sending a metric via HTTP POST

```typescript
import * as https from 'https';

const apiKey = 'YOUR-API-KEY';
const metric = 'foo.ts-http';
const value = 1.2;

const metricData = `${metric} ${value}`;

const basicAuthHeader = 'Basic ' + Buffer.from(apiKey).toString('base64');

https.request({
    hostname: 'www.hostedgraphite.com',
    path: '/api/v1/sink',
    method: 'POST',
    headers: {
        'Content-Type': 'text/plain',
        'Content-Length': metricData.length,
        'Authorization': basicAuthHeader
    }
}, () => {}).end(metricData);
```

### Sending a metric via StatsD

```typescript
import * as dgram from 'dgram';

const socket = dgram.createSocket('udp4');

const apiKey = 'YOUR-API-KEY';
const metricName = 'test.testing-typescript-statsd';
const metricValue = 1.2;

const message = `${apiKey}.${metricName}:${metricValue}|<g/c/ms>`;

socket.send(message, 8125, 'statsd.hostedgraphite.com', () => {
    socket.close();
});
```

As with any TypeScript (.ts) file, compile it to JavaScript using tsc: `tsc filename.ts` and then execute the compiled script using Node.js: `node filename.js`.


---

# 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.hostedgraphite.com/language-guide/typescript.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.
