LoginSignup
23
29

More than 5 years have passed since last update.

BitFlyer Ligntning APIをPythonで使ってみる

Posted at

経緯

BitFlyer Lightningで取引を始めたので,ついでにAPIを触ろうと思った.

APIドキュメント

APIの仕様は
API Documentation

テストは
API Playground

使ってみる

BitFlyer LightningのAPIメニューからkeyとsecretは入手できる.
requestsを使用して使ってみる.

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import json
import requests
import time
import hmac
import hashlib

api_key = 'key'
api_secret = 'secret'
api_endpoint = 'https://api.bitflyer.jp'


def get_api_call(path):
    method = 'GET'
    timestamp = str(time.time())
    text = timestamp + method + path
    sign = hmac.new(api_secret, text, hashlib.sha256).hexdigest()
    request_data=requests.get(
        api_endpoint+path
        ,headers = {
            'ACCESS-KEY': api_key,
            'ACCESS-TIMESTAMP': timestamp,
            'ACCESS-SIGN': sign,
            'Content-Type': 'application/json'
        })
    return request_data


def post_api_call(path,body):
    body = json.dumps(body)
    method = 'POST'
    timestamp = str(time.time())
    text = timestamp + method + path + body
    sign = hmac.new(api_secret, text, hashlib.sha256).hexdigest()
    request_data=requests.post(
        api_endpoint+path
        ,data= body
        ,headers = {
            'ACCESS-KEY': api_key,
            'ACCESS-TIMESTAMP': timestamp,
            'ACCESS-SIGN': sign,
            'Content-Type': 'application/json'
        })
    return request_data

path = '/v1/gethealth'
status = get_api_call(path).json()['status']

print status

GETのクエリなんかはpathに連結させるなり引数に加えるなりしてください.
POSTのbodyはディクショナリオブジェクトでどうぞ.
実行すると,BitFlyer Lightningの状態が取得できます.
だいたいNORMALが返ってくる.
APIのレスポンスはJSONで返ってきますが、ステータスのみ返ってくるAPIもあります.
PubNubを利用したStreamingもできるみたい.

23
29
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
23
29