# 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:** [5월 8, 2019, 10:11오전 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:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Vishal\_Kaushik](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vishal_kaushik/32/140452_2.png) [@Vishal\_Kaushik](https://meta.discourse.org/u/Vishal_Kaushik)
#### Post date: [5월 8, 2019, 10:11오전 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/1 "2019-05-08T10:11:01Z")

</div>

I am trying to create a new topic via Node.js API by sending the following payload :

```
 { title: 'topic:329201',
  raw: 'topic-description:32920111948',
  created_at: '2019-05-08',
  api_key:
   '2371b6549b0c3aeb1f12e49c22db5f25fb1f8ca740619e5304f12f4d29896097',
  api_username: 'cb1' 
}

```

I am hitting the API via HTTP module of node with the following options :

```
{ host: '13.127.14.24',
  port: '3000',
  path: '/posts',
  method: 'POST',
  headers:
   { 'content-type': 'application/x-www-form-urlencoded',
     'content-length': 194,
     Accept: '*/*',
     Connection: 'keep-alive' }
 }

```

But I am getting [“BAD CSRF”] in body. Can anyone help.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [5월 8, 2019, 10:19오전 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/3 "2019-05-08T10:19:31Z")

</div>

You should probably revoke that API key since you just posted it 😉

This is usually because incorrect `content-type` encoding was specified in the request.

---

<div class="post-metadata">

### Author: ![Vishal\_Kaushik](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vishal_kaushik/32/140452_2.png) [@Vishal\_Kaushik](https://meta.discourse.org/u/Vishal_Kaushik)
#### Post date: [5월 8, 2019, 10:22오전 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/4 "2019-05-08T10:22:02Z")

</div>

Don’t worry dude. I am smart enough to randomly change some characters in it.  
And can you specify what i should send in ‘content-type’, as I’ve tried a lot of things.

---

<div class="post-metadata">

### Author: ![Vishal\_Kaushik](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vishal_kaushik/32/140452_2.png) [@Vishal\_Kaushik](https://meta.discourse.org/u/Vishal_Kaushik)
#### Post date: [5월 8, 2019, 10:25오전 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/5 "2019-05-08T10:25:05Z")

</div>

Its neither working with Content-Type:application/x-www-form-urlencoded nor with ‘content-type’:‘application/json’.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [5월 8, 2019, 11:01오전 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/6 "2019-05-08T11:01:11Z")

</div>

You will tend to get “BAD CSRF” if your api key has not been supplied correctly. The best way to send it is via HTTP headers. There are more details at the top of our [api documentation](https://docs.discourse.org/).

---

<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: [5월 8, 2019, 12:37오후 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/10 "2019-05-08T12:37:29Z")

</div>

You might need to have correct capitalization for your header keys. So ‘content-type’ needs to be changed to ‘Content-Type’. Same for ‘Content-Length’, but you probably don’t even need to specify that one.

---

<div class="post-metadata">

### Author: ![michaeld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaeld/32/1594_2.png) [@michaeld](https://meta.discourse.org/u/michaeld)
#### Post date: [5월 8, 2019, 12:47오후 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/11 "2019-05-08T12:47:31Z")

</div>

Fwiw: HTTP header names are (should be) case insensitive per RFC2616 4.2.

---

<div class="post-metadata">

### Author: ![Vishal\_Kaushik](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vishal_kaushik/32/140452_2.png) [@Vishal\_Kaushik](https://meta.discourse.org/u/Vishal_Kaushik)
#### Post date: [5월 8, 2019, 12:50오후 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/12 "2019-05-08T12:50:32Z")

</div>

Problem solved. Thanks to @david. I was sending api\_key in the payload but needed to send it via HTTP headers. I owe you a Beer. 😆

---

<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: [5월 8, 2019, 12:56오후 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/13 "2019-05-08T12:56:00Z")

</div>

Ohh good, glad you got it working! Yes, sending them in the HTTP headers is the correct way to do it now, but they should still work in the body of the POST. Because I’m trying to improve our error messaging when using the API is there anything you changed as well? Also would you be willing to share the actual node http request code? And how are you using something like `querystring.stringify` on your payload? Thanks!

---

<div class="post-metadata">

### Author: ![souljacker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/souljacker/32/115864_2.png) [@souljacker](https://meta.discourse.org/u/souljacker)
#### Post date: [8월 6, 2019, 3:03오후 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/18 "2019-08-06T15:03:54Z")

</div>

Vishal, would you mind sharing the Node code you’re using to make this request?

I’m stuck with a 404 error, no matter what I try.

---

<div class="post-metadata">

### Author: ![Vishal\_Kaushik](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vishal_kaushik/32/140452_2.png) [@Vishal\_Kaushik](https://meta.discourse.org/u/Vishal_Kaushik)
#### Post date: [8월 19, 2019, 9:43오전 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/19 "2019-08-19T09:43:57Z")

</div>

@souljacker Sorry for the late reply. Were you able to get it working?

---

<div class="post-metadata">

### Author: ![Konrad\_Sopala](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/konrad_sopala/32/167014_2.png) [@Konrad\_Sopala](https://meta.discourse.org/u/Konrad_Sopala)
#### Post date: [3월 30, 2020, 7:45오전 UTC](https://meta.discourse.org/t/creating-a-topic-via-the-api-using-node-js/117160/20 "2020-03-30T07:45:20Z")

</div>

Blake, 엔지니어링 팀에서 Node.js로 동일한 요청을 보내는 방법에 대해 나에게도 안내해 줄 사람이 있을까요? 이 Python 스레드에 공개적으로 올리고 싶지 않다면 DM으로 이야기할 수 있어요. 정말 감사합니다!

---

<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: [3월 30, 2020, 12:44오후 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)를 수정하여 node.js를 사용하여 discourse에 POST 요청을 보내는 방법을 시연합니다:

```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();

```
