Newbie API user, create a record

hey knack guys,

looking pretty cool, but having a hard time getting started.  i have a client who's considering using knack with HIS clients, and i've been asked to look into this, mainly the API.  just trying to start by creating a simple record in a tiny test database.

took the cURL code from the API documentation, plugged it in at Postman, clicked send and it updated my database.  tried converting to javascript in postman, but i'm seeing now that javascript is probably not going to work due to cross-domain issues.  so...  converted it to php at Postman, but it's just throwing errors.

including the cURL code that works from Postman, and the php code it gives me. and i'm assuming that i can't use the cURL code as i seriously doubt any of my client's clients are going to have it installed.

anyway, just looking for some simple snippet that i can put in to my javascript of php that will allow me to create a record.  thanks in advance for any help you can shove my way:)

 

php code from postman
=============================

<?php
$request = new HttpRequest();
$request->setUrl('https://flash.knack.com/bob1/objects/object_1/records');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'Postman-Token' => '95380af0-9cc7-c9f4-a9d8-f5b058d8a286',
  'Cache-Control' => 'no-cache',
  'Content-Type' => 'application/json',
  'X-Knack-REST-API-Key' => 'aaa109e0-ff95-11e7-a797-2d51a60fe294',
  'X-Knack-Application-Id' => '5a6615607dea85588cd0bf06',
));

$request->setBody('{
        "field_1": "giraffe"
      }');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}
?>

 

 

There's also a node.js for Knack but I haven't tried it.

 

https://www.npmjs.com/package/knackhq-client

hey justin,

man, thanks for your time.  even though it's not all sinking in yet:)  so my number 1 newbie look starts with...  hahaha, every entry and comment in this here thread has a little gear after it except for the first one.  which is the one i wanted to edit.  but like i said, that's ok.  at the moment, this is just my personal trial period so this whole setup may go away anyway.

so, i'm a pretty good javascript guy, some experience with php, but not much experience with ajax.  when i was asked to look at this, all i wanted to do was complete a couple of successful api calls so i could report that this should work.  one call to create records, and probably one to retrieve them.  the knack doc pages have those curl examples and talked about postman, so i copied the "create record" one, pasted it in to postman, ran it, and voila, it updated my database.  have been exporting javascript (preferred) and php from postman and plugging it into my code ever since and have not gotten one successful write yet.  oh yeah, and i updated the pages so it's all cleaner, and yes, i had read about view-based call which is what i'm using now.

ooh, if you want to see the javascript one not work:

http://files.nfnitude.com/z_gb/_knack2/knack_javascript.html

the page will show you the code.  in firefox i don't see anything else unless i open the console where i see a "cross-origin request blocked" message.  that all made me think i couldn't do it via jsvascript without making the client do something on his side, so i started trying php.  so yes, there's a knack_php.html page there, too.

thanks again, justin.

 

 

 

 

You can click the gear to edit. You might as well just copy the whole app at some point and then use that one as your permanent site since these keys were exposed at one point. It will cost you very little time. 

Something to consider when writing API calls is that your key is actually exposed to any user who has a basic knowledge of web browsers who has access to your site -if you are doing an object based call that originates in the javascript panel. So, if you go the ajax route -I'd recommend doing a 'view based call' ( Basically, using the user's logged in token to perform actions that they could perform with their token in their browser. ie submit a form in the background.) 90% of what you'll want to do can be accomplished this way.

If you continue with the Curl/Server route than this is not important. But, it's way easier in my opinion to make ajax calls. Just grab some code, pop it into your javascript panel and amend it to your needs. Many examples on these forums and in the Knack developer docs to get you started.

People on these forums will help and I also learned a lot using the site codementor. While mentor's can be pricey, if they're teaching you while at the same time solving your problems it can be worth it. 

Best of luck on your Coding Future. It's surprisingly fun.

 

hey justin,

thanks.  well, i don't have much experience with either (never heard of curl before), but i'm actually trying to get either ajax or php to work for this, but still no luck.  the curl part actually works from postman, but i can't use it.  not installed on my server?

as to the api keys, thanks again, and i had considered that when i wrote it.  just trying to make sure it works, and yes, this will probably just be a test trial account anyway.  but by the way, i'm in here now and...  haha, how do i edit the previous post to take the id stuff out?  i only saw this thing about replying.

thanks again,

Hi Sk,

Hit me up for advice on Ajax requests. I don't really have a lot of experience using Curl.

PS Please update your post and hash out your api key. Unless you plan on starting a brand new account when you're done testing -- you'll really want to keep that stuff secret.