API使用例¶
[更新: 2022年4月21日]
「さくらのモノプラットフォーム」関連のAPIの使用例です。
重要
さくらのクラウドAPIの利用方法など、一般的な情報については APIドキュメント を参照ください。
プロジェクト関連¶
ヒント
ゾーン共通リソースのため石狩第1ゾーン(is1a)のエンドポイントの利用を推奨します。
プロジェクト作成¶
| Name | プロジェクト名(必須) | 1 ~ 64文字 | 
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X POST \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem \
  -d '{
  "CommonServiceItem": {
    "Name": "プロジェクト名",
    "Provider": {
      "Class": "iotplatform"
    },
    "ServiceClass": "cloud/iotplatform/1"
  }
}'
レスポンス例
{
  "CommonServiceItem": {
    "ID": "xxxxxxxxxxx",
    "Name": "xxxxxxxxx",
    "Description": null,
    "Settings": null,
    "SettingsHash": null,
    "Status": null,
    "ServiceClass": "cloud/iotplatform/vmgw/1",
    "Availability": "available",
    "CreatedAt": "2022-03-30T16:52:26+09:00",
    "ModifiedAt": "2022-03-30T16:52:26+09:00",
    "Provider": {
      "ID": 5130001,
      "Class": "iotplatform",
      "Name": "iotplatform",
      "ServiceClass": "cloud/iotplatform"
    },
    "Icon": null,
    "Tags": []
  },
  "Success": true,
  "is_ok": true
}
プロジェクト一覧の取得¶
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/ \
  -X GET \
  --data-urlencode '{"From":0,"Sort":["ID"],"Filter":{"Provider.Class":"iotplatform"}}'
レスポンス例
{
   "From": 0,
   "Count": 6,
   "Total": 6,
   "CommonServiceItems": [
     {
       "Index": 0,
       "ID": "xxxxxxxxxxx",
       "Name": "xxxxxxxxx",
       "Description": "",
       "Settings": null,
       "SettingsHash": null,
       "Status": {
         "mgw_id": "xxxxxxxxxxx",
         "zone": "is1z",
         "Services": [
           "113300025454",
           "113300125524"
         ],
       },
     }
    ],
    "is_ok": true
}
プロジェクト情報の取得¶
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X GET \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJRCT_ID
レスポンス例
{
  "CommonServiceItem": {
    "ID": "xxxxxxxxxxx",
    "Name": "xxxxxxxx",
    "Description": null,
    "Settings": null,
    "SettingsHash": null,
    "Status": null,
    "ServiceClass": "cloud/iotplatform/vmgw/1",
    "Availability": "available",
    "CreatedAt": "2022-03-30T16:53:30+09:00",
    "ModifiedAt": "2022-03-30T16:53:30+09:00",
    "Provider": {
      "ID": "5130001",
      "Class": "iotplatform",
      "Name": "iotplatform",
      "ServiceClass": "cloud/iotplatform"
    },
    "Icon": null,
    "Tags": []
  },
  "is_ok": true
}
プロジェクト削除¶
注意
プロジェクトに接続しているSIM及びサービスアダプタが存在しない場合のみ削除できます。
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X DELETE \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID
レスポンス例
{
    "CommonServiceItem": {
        "ID": "xxxxxxxxx",
        "Name": "xxxxxx",
        "Description": null,
        "Settings": null,
        "SettingsHash": null,
        "Status": null,
        "ServiceClass": "cloud/iotplatform/vmgw/1",
        "Availability": "discontinued",
        "CreatedAt": "2022-03-30T16:53:30+09:00",
        "ModifiedAt": "2022-03-30T16:53:30+09:00",
        "Provider": {
            "ID": "5130001",
            "Class": "iotplatform",
            "Name": "iotplatform",
            "ServiceClass": "cloud/iotplatform"
        },
        "Icon": null,
        "Tags": []
    },
    "Success": true,
    "is_ok": true
}
プロジェクトへのデバイス接続関連¶
注意
SIM(セキュアモバイルコネクト)をプロジェクトに接続する場合以下の手順で行います。
- 「SIM(セキュアモバイルコネクト)をプロジェクトに接続」APIにプロジェクトIDとSIMIDをつけてリクエストを送ります。
- 「SIM(セキュアモバイルコネクト)のIPを設定」にリクエストを送ることでSIMにIPが割り当てられます。
SIM(セキュアモバイルコネクト)をプロジェクトに接続¶
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X PUT \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/sims/$SIM_ID
レスポンス例
{
  "IoTPlatform": {
    "mgw_id": "xxxxxxxxxxxx",
    "zone": "xxxx"
  },
  "is_ok": true
}
SIM(セキュアモバイルコネクト)のIPを設定¶
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X PUT \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/sims/$SIM_ID/ip
レスポンス例
{
  "IoTPlatform": {
    "sim_id": "xxxxxxxxx",
    "ip_address": "xxxxxxx",
    "device_id": "xxxxxxxxx"
  },
  "is_ok": true
}
プロジェクトに紐づくSIM(セキュアモバイルコネクト)を取得¶
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X GET \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/113400634403/iotplatform/vmgw/sims
レスポンス例
{
   "IoTPlatform": [
     {
       "sim_id": "xxxxxxxxxx",
       "ip_address": "xxxxxxxx",
       "device_id": "xxxxx"
     }
   ],
   "is_ok": true
 }
SIM(セキュアモバイルコネクト)をプロジェクトから解除¶
注意
SIM(セキュアモバイルコネクト)をプロジェクトから解除する場合以下の手順で行います。
- 「SIM(セキュアモバイルコネクト)のIPを解除」APIにリクエストしてIPアドレスを解除します。
- 「SIM(セキュアモバイルコネクト)のプロジェクト接続を削除」APIにリクエストし解除します。
SIM(セキュアモバイルコネクト)のIPを解除¶
注意
IPを解除してもプロジェクトに再接続した場合、切断前のipアドレスがSIMに割り振られます。
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X DELETE \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/sims/$SIM_ID/ip
レスポンス例
{
  "IoTPlatform": {
    "sim_id": "xxxxxxxx",
    "ip_address": "xx.xx.xx.xx",
    "device_id": "xxxxx"
  },
  "is_ok": true
}
SIM(セキュアモバイルコネクト)のプロジェクト接続を削除¶
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X DELETE \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/sims/$SIM_ID
レスポンス例
{
  "IoTPlatform": {
    "sim_id": "xxxxxxxx",
    "mgw_id": "xxxxxxxx",
    "zone": "xxxx"
  },
  "is_ok": true
}
サービスアダプタ関連¶
サービスアダプタ作成(WebSocket)¶
| name | サービスアダプタ名(任意) | 1 ~ 64文字 | 
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X POST \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/services \
  -d '{
  "IoTPlatform": {
    "type": "websocket",
    "name": "サービスアダプタ名",
    "settings": {
      "websocket": {}
    }
  }
}'
レスポンス例
{
  "IoTPlatform": {
    "id": "xxxxxxx",
    "name": "xxxxxxx",
    "type": "websocket",
    "vmgw": "xxxxxxx",
    "token": "xxxxxxxxxxxxxxxxxxxxx",
    "url": "https://ws.sipf.iot.sakura.ad.jp/v0/xxxxxxxxxxxxxxxxxxxxx",
    "settings": {
      "incoming-webhook": [],
      "outgoing-webhook": [],
      "websocket": []
    }
  },
  "is_ok": true
}
サービスアダプタ作成(Incoming Webhook)¶
| name | サービスアダプタ名(任意) | 1 ~ 64文字 | 
| secret | シークレット文字列(任意) | 1 ~ 64文字(英数字) | 
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X POST \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/services \
  -d '{
  "IoTPlatform": {
    "type": "incoming-webhook",
    "name": "サービスアダプタ名",
    "settings": {
      "incoming-webhook": {
        "secret": "シークレット文字列"
      }
    }
  }
}'
レスポンス例
{
  "IoTPlatform": {
    "id": "xxxxxxx",
    "name": "xxxxxxx",
    "type": "incoming-webhook",
    "vmgw": "xxxxxxx",
    "token": "xxxxxxxxxxxxxxxxxxxxx",
    "url": "https://incoming.sipf.iot.sakura.ad.jp/v0/xxxxxxxxxxxxxxxxxxxxx",
    "settings": {
      "incoming-webhook": {
        "secret": "xxxxxxx"
      },
      "outgoing-webhook": [],
      "websocket": []
    }
  },
  "is_ok": true
}
サービスアダプタ作成(Outgoing Webhook)¶
| name | サービスアダプタ名(任意) | 1 ~ 64文字 | 
| secret | シークレット文字列(任意) | 1 ~ 64文字(英数字) | 
| url | 宛先URL(必須) | http or httpsから始まるURL | 
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X POST \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/services \
  -d '{
  "IoTPlatform": {
    "type": "outgoing-webhook",
    "name": "サービスアダプタ名",
    "settings": {
      "outgoing-webhook": {
        "secret": "シークレット文字列",
        "url": "宛先URL"
      }
    }
  }
}'
レスポンス例
{
  "IoTPlatform": {
    "id": "xxxxxxx",
    "name": "サービスアダプタ名",
    "type": "outgoing-webhook",
    "vmgw": "xxxxxxx",
    "token": "xxxxxxxxxxxxxxxxxxxxx",
    "url": "宛先URL",
    "settings": {
      "outgoing-webhook": {
        "secret": "シークレット文字列"
      },
      "incoming-webhook": [],
      "websocket": []
    }
  },
  "is_ok": true
}
サービスアダプタ一覧の取得¶
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X GET \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/services
レスポンス例
{
  "IoTPlatform": [
  {
    "id": "xxxxxxxxxx",
    "name": "xxxxxxx",
    "type": "websocket",
    "vmgw": "xxxxxxxxxx",
    "token": "xxxxxxxxxxxxxxxxxxxx",
    "url": "wss://ws.sipf.iot.sakura.ad.jp/v0/xxxxxxxxxxxxxxxxxxxx",
    "settings": {
      "incoming-webhook": [],
      "outgoing-webhook": [],
      "websocket": []
    }
  }
  ],
  "is_ok": true
}
サービスアダプタの削除¶
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X DELETE \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/services/$SERVICEADAPTER_ID
レスポンス例
{
  "IoTPlatform": {
    "service_id": "xxxxxxxx"
  },
  "is_ok": true
}
ファイル送受信関連¶
ファイルアップロード¶
注意
ファイルアップロードは以下の手順で行います。
- ファイルID作成APIでファイルIDを作成します。
- 作成済みのファイルIDでファイル送信リクエストAPIにリクエストを送ります。
- 2で返ってきたレスポンスのurlとtokenのプロパティで、ファイルアップロードAPIにファイルパスをつけてリクエストを送ります。
ファイルID作成¶
ヒント
ファイルIDは「英数字始まり, 英字, 数字, 一部記号(_.), 1~32文字」を許容しています。
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X PUT \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/files/$FILE_ID/stat
レスポンス例
{
  "IoTPlatform": {
    "name": "$FILE_ID",
    "vmgw": "xxxxxxxxx",
    "mime_type": "",
    "size": 0,
    "created_at": "2022-02-18T07:01:03.814008Z",
    "updated_at": "2022-04-01T05:45:42.060480Z"
  },
  "is_ok": true
}
ファイルアップロードリクエスト¶
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X PUT \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/files/$FILE_ID/upload_request
レスポンス例
{
  "IoTPlatform": {
    "url": "https://api.sakura.io/v1/cloud/vmgws/xxxxxxxx/files/$FILE_ID//upload/",
    "token": "xxxxxxx"
  },
  "is_ok": true
}
ファイルアップロード¶
| attachment | ファイルのパス(必須) | 100MB以下を推奨 | 
curlコマンド例
curl -H 'Authorization: Bearer $TOKEN' -X PUT \
  -F attachment=@ファイルのパス \
  https://api.sakura.io/v1/cloud/vmgws/$PROJECT_ID/files/$FILE_ID/upload/
レスポンス例
{
   "name":"xxxxx",
   "vmgw":"xxxxxxxxxx",
   "mime_type":"application/octet-stream",
   "size":41289,
   "created_at":"2022-04-05T07:35:45.489624Z",
   "updated_at":"2022-04-05T07:35:47.125109Z"
}
ファイルダウンロード¶
注意
ファイルのダウンロードは以下の手順で行います。
- ファイルダウンロードAPIにアクセスします。レスポンスのurlプロパティにダウンロード用の一時URLが入っています。
- 一時URLにGETリクエストを送ります。レスポンスがファイルの中身です。
※ 一時URLには有効期限があるのでダウンロード毎にファイルダウンロードAPIにリクエストしてください。
curlコマンド例
curl -u "$SAKURACLOUD_ACCESS_TOKEN:$SAKURACLOUD_ACCESS_TOKEN_SECRET" \
  -X GET \
  https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/commonserviceitem/$PROJECT_ID/iotplatform/vmgw/files/$FILE_ID/download
レスポンス例
{
  "IoTPlatform": {
    "name": "xxxxxxxx",
    "vmgw": "xxxxxxxx",
    "mime_type": "application/octet-stream",
    "url": "https://xxxxxxxx/xxxxxxxx",
    "size": 41289
  },
  "is_ok": true
}
