LoginSignup
3
1

More than 5 years have passed since last update.

Node.jsからSORACOM APIを使用する

Posted at

Node.jsからsoracomのAPIを使用してみようと思ったら公開されているライブラリが動かなかったので作りました。

Node.jsの'request'パッケージからSORACOMのAPIを叩くと、何故かエラーが返ってきてしまい解決できなかったので、'https'のパッケージを使用して作りました。

SORACOM APIについて

インストール方法

npm install soracom_api

使い方

"authKey"と"token"はAPIにアクセスするときに自動的に取りに行きます。

認証

emailによる認証と、authKeyによる認証の双方に対応しています。

var soracom = new Soracom({email: 'mail address',password:'password'});
var soracom = new Soracom({authKeyId: 'keyId',authKey:'secret'});

When this API accesses SORACOM service, it takes authKey and token automatically.

(例) IMSIの情報を取得する

var Soracom = require('soracom_api');
var soracom = new Soracom({email: 'mail address',password:'password'});
soracom.get('/subscriber/imsi',function(err,res){
  console.log({err:err,res:res});
});

(例) SIMに名前をセットする

var Soracom = require('soracom_api');
var soracom = new Soracom({authKeyId: 'keyId',authKey:'secret'});
soracom.put('/subscriber/imsi/tags',[
  {
    "tagName": "name",
    "tagValue": "my_test_sim"
  }
],function(err,res){
  console.log({err:err,res:res});
});

API

soracom.get('path',function(err,res){ callback });

parameters:
- path: リクエストのパスを指定します
return:
- err: エラーメッセージ、正常時はnull
- res: 正常時にレスポンスが返ります

soracom.post('path',params,function(err,res){ callback });

  • parameters:
    • path: リクエストのパスを指定します
    • params: リクエストのbodyを設定します。bodyが無いときは省略可能です。
  • return:
    • err: エラーメッセージ、正常時はnull
    • res: 正常時にレスポンスが返ります

soracom.put('path',params,function(err,res){ callback });

  • parameters:
    • path: request path
    • params: リクエストのbodyを設定します。bodyが無いときは省略可能です。
  • return:
    • err: エラーメッセージ、正常時はnull
    • res: 正常時にレスポンスが返ります

soracom.delete('path',params,function(err,res){ callback });

  • parameters:
    • path: request path
    • params: リクエストのbodyを設定します。bodyが無いときは省略可能です。
  • return:
    • err: エラーメッセージ、正常時はnull
    • res: 正常時にレスポンスが返ります
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1