# Creating a topic via the API using Node.js

**URL:** <https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160>\
**Category:** Development\
**Created:** [08.Май.2019 10:11:01 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160 "2019-05-08T10:11:01Z")\
**Posts on this page:** 1\
**Showing post:** 21

<div class="post-metadata">

**Author:** ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)\
**Post date:** [30.Март.2020 12:44:43 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/21 "2020-03-30T12:44:43Z")

</div>

Я адаптировал [пример с сайта node.js](https://nodejs.dev/make-an-http-post-request-using-nodejs), чтобы продемонстрировать, как отправить POST-запрос в Discourse с помощью node.js:

```plaintext
const http = require('http');
const crypto = require('crypto');
const querystring = require('querystring');

const title = crypto.randomBytes(6).toString("hex") + ' ' + crypto.randomBytes(3).toString("hex") + ' ' + crypto.randomBytes(4).toString("hex");
const raw = crypto.randomBytes(16).toString("hex") + ' ' + crypto.randomBytes(16).toString("hex") + ' ' + crypto.randomBytes(16).toString("hex");

const api_key = 'f2b43adc...';
const api_username = 'system';

var data = {
  title: title,
  raw: raw
};

data = JSON.stringify(data);

const options = {
  hostname: 'localhost',
  port: 3000,
  path: '/posts',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': data.length,
    'Api-Key': api_key,
    'Api-Username': api_username
  }
};

const req = http.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`)

  res.on('data', (d) => {
    process.stdout.write(d)
  })
});

req.on('error', (error) => {
  console.error(error)
});

req.write(data);
req.end();

```

---

_[View the full topic](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160)._
