# 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:** 4
**Page:** 1

<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, 8:26am UTC](https://meta.discourse.org/t/create-topic-using-discourse-api/243930/1 "2022-11-02T08:26:09Z")

</div>

Hello  
I need your help please.  
I’m trying to create a topic using Discourse API but it’s fail every time I tried. This is my code :

```plaintext
https://myDiscourse.com/posts.json?title=New+test+posted&raw=new+posted+text+to+test+creation+by+api&category=45&api_key=xxxxxxxxxxxxxxxxxxxxxxxx&api_username=admin

```

I don’t know what is wrong, can you help me please?

Thanks

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [November 2, 2022, 10:45am UTC](https://meta.discourse.org/t/create-topic-using-discourse-api/243930/2 "2022-11-02T10:45:38Z")

</div>

You can’t include the API key in the url (it exposes your key you anyone who can see your web traffic). They key must be passed in the headers.

See [Grant a custom badge through the API](https://meta.discourse.org/t/grant-a-custom-badge-through-the-api/103270) for an example.

---

<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

---

<div class="post-metadata">

### Author: ![michaelhenderson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaelhenderson/32/76069_2.png) [@michaelhenderson](https://meta.discourse.org/u/michaelhenderson)
#### Post date: [November 3, 2022, 12:19pm UTC](https://meta.discourse.org/t/create-topic-using-discourse-api/243930/4 "2022-11-03T12:19:19Z")

</div>

Thanks for the code. It’s hard to debug it fully without seeing all of the code. I would recommend querying the API outside of the script itself. For example, you could use curl

```plaintext
curl -X GET "https://mydiscourse.com/posts.json" \
-H "Api-Key: 714552c6148e1617aeab526d0606184b94a80ec048fc09894ff1a72b740c5f19" \
-H "Api-Username: michael"

```

If you’re not comfortable with the command line or using curl, I would recommend using Postman to debug the issue with the API. Postman is essential for me as a developer when testing or building APIs.

Here is a Postman workspace with Discourse routes already setup. Just configure your API key and username.  
[Discourse API Documentation Postman API Network](https://www.postman.com/api-evangelist/workspace/discourse/documentation/35240-c739e2a5-3ddf-4ffc-90e9-7dd579e8a55d)
