Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
29
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@earshttp

BitFlyer Ligntning APIをPythonで使ってみる

経緯

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もできるみたい.

29
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
29
Help us understand the problem. What is going on with this article?