KAS 사용하기 - 기본 세팅
KAS를 사용하여 BAPP을 개발하기 앞서 모든 API가 그렇듯 해당 Document를 참고하여 개발을 한다.
KAS또한 Document가 굉장히 잘 정리되어 있고 개발자 포럼에도 직원들이 24시간 내에 빠르고 친절이 답해준다.
(기본적으로 BApp, DApp들은 주로 java와 java script를 지원하는데 이중 java script 언어에 대한 지원이 더 잘 돼있기 때문에 js 위주로 글을 작성한다.)
Introduction
이 페이지는 Klaytn API Service (KAS)를 소개합니다.
docs.klaytnapi.com
https://forum.klaytn.com/c/kas/25
KAS
This category is for questions related to Klaytn API Service (KAS). KAS is an API service for easier and quicker blockchain service development.
forum.klaytn.com
1. SDK 설치
$ npm install caver-js
$ npm install caver-js-ext-kas
2. apiCaller.js 작성
const request = require('request')
class ApiCaller {
constructor(_endpoint) {
this.endpoint = _endpoint;
}
async call(options){
options.url = this.endpoint + options.url;
options.json = true;
if(!options.headers) options.headers = {};
options.headers['x-chain-id'] = '1001';
options.headers['content-type'] = 'application/json';
options.headers.Authorization = ' '
return new Promise((resolve, reject) => {
request(options,function(error, _response, body){
if(error) reject(error);
else resolve(body);
})
})
}
}
module.exports = ApiCaller;
KAS에서 제공하는 API를 호출할때 header 부분에는 x-chain-id , content-type, Authorization이 있는데
이를 apiCaller에서 미리 정의해두고 사용하려는 api를 호출하는 파일들을 상속시켜 사용할 예정이다.
x-chain-id : 클레이튼 네트워크의 번호이며 메인 네트워크(Cypress)는 8217, 테스트 네트워크(Baobab)는 1001
Authorization : KAS Console에서 생성한 Authorization id 를 입력한다.