Assignment 501 -- making an API call from Apiant for the first time

Hi,

I’m trying to make an API call to integrate Discourse with Apiant. My goal is to eventually make a new post to the Apiant forum, which uses Discourse, through an Apiant assembly or automation.

I’m basing my Discourse integration on the existing integration with123FormBuilder. 123FormBuilder has variable for a URL and something called an apiKey. In 123FormBuilder’s script module, I see a line

$resp = curl_exec($curl);

That’s the API call, correct? If so, how does the apiKey variable fit into that – is apiKey identifying a particular Discourse method or function, or is it serving a security purpose?

It’s not clear to me how the API call is distinguishing between methods and establishing security, it sounds like “apiKey” has a security purpose, but it seems to be used as a method/function identifier?

I think my question betrays my ignorance of web things and PHP in general; I don’t think it’s an Apiant specific question, but Robert asked me to post in the forum anyway.

Thanks,
Ben

API documentation will have a section talking about authentication. Here it is for Discourse: https://docs.discourse.org/#system-releases

Yes, curl_exec makes the API call.

In the 123FormBuilder assembly, a URL is being built that contains the API key, then that is passed to the PHP code which invokes the URL and receives the response:

Based on the Discourse documentation, you need to do it the same way, except the key is passed in a query parameter named “api_key”. You also seemingly need to pass a value for “api_username”. For the API key I provided you, I think any value will work, maybe it is just used to identify access to the API. If that is true, then use the “appName” field emitted by the System Info module to use the application name for the value.

Full knowledge of URL components, HTTP verbs, and the basics of making API calls are prerequisites for using the system. You will need to read up. Here is a good intro from one of our competitors: https://zapier.com/learn/apis/

Hi Robert,

OK, thanks for that Zapier intro, it was extremely informative.

I’m able to successfully call a couple of the Discourse endpoints at forum.apiant.com using an HTTP client; I stuck with GET requests in order to keep the complexity down. However, attempting to do the same (I think?) through an Extension Sever-side Script module in my assembly for 501 doesn’t seem to do the same. I doubt this is an issue with Apiant, but any suggestion you can offer is appreciated.

I’ll move on to prototyping a POST request to “post new topic” in an HTTP client, as you suggested in your e-mail (not shown in the forum, for any other readers).

Thanks,
Ben

I don’t understand what you mean re: server-side script not working, need to see examples of what works and what doesn’t.

OK. I will not be able to work on this until next week, so I’ll write a post then. Thanks! :smiley:

Hi Robert,

Thanks for responding to my last post…and thanks for directing me to that Zapier resource. I should have learned that stuff 10 years ago.

How about this issue: I finally called the “Create a new topic, a new post, or a private message” API for the Apiant forum. However, I get a 403: invalid_access response back. The error says,

“You are not permitted to view the requested resource. The API username or key is invalid.”

I copied-and-pasted the api_key you supplied me with via e-mail, and for api_username I used “discourse1”.
What do you think? Does that api_key not have permission somehow to make a post, or did we somehow miss a digit in that api_key, or maybe I’m using the wrong api_username?

Thanks,
Ben

Err – I figured that I was using the wrong api_username, and so I just guessed the username, and I got it right.

It seems like my next question might be strictly Discourse related, so I’ll browse the forum on Discourse next.

Sounds like your app assembly needs to prompt for entry of the API Username value after all.

Poor API documentation is the bane of integration.

I suppose…well, I’m getting a 422 now, and I don’t think it’s your issue, so I went over to the Discourse meta forum and posted the following:

Hopefully someone gets back to me on this – unless you know how to deal with the 422? Do you know of a setting on your side to deactivate “title checking,” maybe?

Generally you have to believe that error codes are the result of doing something incorrectly rather than the API being broken.

This post suggests a 422 when creating posts is due to rate limiting: https://meta.discourse.org/t/discourse-always-posts-in-general-using-api/70276

Try using curl or a tool like Postman to help troubleshoot. Try to find an example online of the API call working with one of these tools. The best API’s will document themselves with working examples, but I didn’t see any in the Discourse documentation.

Here’s an example I found for creating posts via curl: https://meta.discourse.org/t/api-post-creation-breaks-if-post-contains-an-ampersand/48002

Yes, I agree that’s an important truth.

In this case, I’ve noticed that I was using content-type = urlencoded, but I wasn’t specifying the title in URL parameters. I had specified all the data in the body. Ooops!