Code Examples
All of the examples below will return the partNumber
, description
, retailPrice
, and quantityAvailable
fields for a BB62. Make sure to replace <YOUR TOKEN>
with your own token.
cURL
curl -X "POST" "https://api.sct-usa.com/api/graphql" \
-H 'Authorization: Bearer <YOUR TOKEN>' \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"query": "{ tools(partNumber:\\"BB62\\"){ partNumber description retailPrice quantityAvailable } }"
}'
C#
using System;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Text;
namespace MyNamespace {
public class MyActivity {
private async Task<bool> SCTAPI () {
string url = "https://api.sct-usa.com/api/graphql";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (new Uri(url));
request.ContentType = "application/json; charset=utf-8";
request.Headers.Add("Authorization", "Bearer <YOUR TOKEN>");
request.Method = "POST";
string postData = "{\"query\":\"{ tools(partNumber:\\\"BB62\\\"){ partNumber description retailPrice quantityAvailable } }\"}";
ASCIIEncoding encoding = new ASCIIEncoding ();
byte[] byte1 = encoding.GetBytes (postData);
request.ContentLength = byte1.Length;
Stream newStream = request.GetRequestStream ();
newStream.Write (byte1, 0, byte1.Length);
newStream.Close ();
using (WebResponse response = await request.GetResponseAsync ()) {
using (Stream stream = response.GetResponseStream ()) {
return true;
//process the response
}
}
}
}
}
PHP + cURL
<?php
// get cURL resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, 'https://api.sct-usa.com/api/graphql');
// set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
// return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer <YOUR TOKEN>',
'Content-Type: application/json; charset=utf-8',
]);
// json body
$json_array = [
'query' => '{ tools(partNumber:"BB62"){ partNumber description retailPrice quantityAvailable } }'
];
$body = json_encode($json_array);
// set body
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
// send the request and save response to $response
$response = curl_exec($ch);
// stop if fails
if (!$response) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;
// close curl resource to free up system resources
curl_close($ch);
JavaScript (fetch)
fetch('https://api.sct-usa.com/api/graphql', {
method: 'POST',
headers: {
'Authorization': 'Bearer <YOUR TOKEN>',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"query": "{ tools(partNumber:\"BB62\"){ partNumber description retailPrice quantityAvailable } }"
}),
})
.then(res => res.json())
.then(res => console.log(res.data));
JavaScript (jQuery)
jQuery.ajax({
url: "https://api.sct-usa.com/api/graphql",
type: "POST",
headers: {
"Authorization": "Bearer <YOUR TOKEN>",
"Content-Type": "application/json; charset=utf-8",
},
contentType: "application/json",
data: JSON.stringify({
"query": "{ tools(partNumber:\"BB62\"){ partNumber description retailPrice quantityAvailable } }"
})
})
.done(function(data, textStatus, jqXHR) {
console.log("HTTP Request Succeeded: " + jqXHR.status);
console.log(data);
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log("HTTP Request Failed");
})
.always(function() {
/* ... */
});