# Create topic using Discourse API

**URL:** <https://meta.discourse.org/t/create-topic-using-discourse-api/243930>\
**Category:** Development\
**Tags:** rest-api\
**Created:** [November 2, 2022, 8:26am UTC](https://meta.discourse.org/t/create-topic-using-discourse-api/243930 "2022-11-02T08:26:09Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![alexcomp](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@alexcomp](https://meta.discourse.org/u/alexcomp)\
**Post date:** [November 2, 2022, 11:16pm UTC](https://meta.discourse.org/t/create-topic-using-discourse-api/243930/3 "2022-11-02T23:16:44Z")

</div>

Hello @pfaffman  
Thank you for your help. I tried according to example but there is a error :

```plaintext
401 Authorization Required

```

My script :

```plaintext
$apikey = "xxxxxxxxxxxxxxxxxxxx";
	$username = "xxxxxxxxxxxxxxxxx";
	
	$title = "test";
	$raw = "message";
	$category = 9;
	
	$url = 'https://myDiscourse.com/posts.json';
	$post_fields = array(
		'title' => $title,
		'raw' => $raw,
		'category' => $category,
	);
	$headers = array("Content-Type: multipart/form-data;","Api-Key: $apikey","Api-Username: $username",);

	$ch = curl_init();
	curl_setopt( $ch, CURLOPT_URL, $url );
	curl_setopt( $ch, CURLOPT_POST, 1 );
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
	curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $post_fields ) );
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

	$result = curl_exec( $ch );

	if ( curl_errno( $ch ) !== 0 ) {
		echo 'Error:' . curl_error($ch);
	}

	curl_close( $ch );

```

I made a mistake certainly but I don’t know what. Can you help me please?

Thanks

---

_[View the full topic](https://meta.discourse.org/t/create-topic-using-discourse-api/243930)._
