stripe API 使い方 (List all customers)

API の仕様はこちら
[List all customers](https://stripe.com/docs/api/customers/list)

Ubuntu 21.10 で確認しました。
#データの準備#

CLI のインストール

```bash
wget https://github.com/stripe/stripe-cli/releases/download/v1.7.9/stripe_1.7.9_linux_amd64.deb
sudo dpkg -i stripe_1.7.9_linux_amd64.deb
```

インストールの確認

```text
$ stripe --version
stripe version 1.7.9
```

カスタマーの作成

```bash
stripe customers create
```

Secret Key の取得
https://dashboard.stripe.com/test/dashboard
![stripe_aa.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/ca2ff789-39e4-02ba-c0d7-715a9da3e729.png)

.env の作成

```text:.env
SECRET_KEY="sk_test_51K7DKjJ*****"
```

#stripe cli でカスタマーの一覧を取得#
>stripe login を行う必要があります。

```bash:get_customers_cli.sh
#! /bin/bash
#
#	get_customers_cli.sh
#
#				Dec/19/2021
#
#
stripe customers list --limit=10 > customers.json
#
jq .data[].id customers.json
```

実行結果

```text
$ ./get_customers_cli.sh 
"cus_KmrIU4hzWqYt7R"
"cus_KmoG4k1wiKxSDc"
"cus_KmnqmJhBAvPGMq"
"cus_KmnpiRFjm8he97"
```

#Curl でカスタマーの一覧を取得#

```bash:get_customers_curl.sh
#! /bin/bash
#
#	get_customers_curl.sh
#
#						Dec/16/2021
#
. .env
#
curl https://api.stripe.com/v1/customers \
  -u $SECRET_KEY":" > customers.json
#
jq .data[].id customers.json
#
```

実行結果

```text
$ ./get_customers_curl.sh 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2850  100  2850    0     0   4298      0 --:--:-- --:--:-- --:--:--  4298
"cus_KmrIU4hzWqYt7R"
"cus_KmoG4k1wiKxSDc"
"cus_KmnqmJhBAvPGMq"
"cus_KmnpiRFjm8he97"
```

#Httpie でカスタマーの一覧を取得#

```bash:get_customers_http.sh
#! /bin/bash
#
#	get_customers_http.sh
#
#				Dec/16/2021
#
. .env
#
http https://api.stripe.com/v1/customers \
  --auth $SECRET_KEY":" > customers.json
#
jq .data[].id customers.json
```

実行結果

```text
$ ./get_customers_http.sh 
"cus_KmrIU4hzWqYt7R"
"cus_KmoG4k1wiKxSDc"
"cus_KmnqmJhBAvPGMq"
"cus_KmnpiRFjm8he97"
```

#Python requests でカスタマーの一覧を取得#

```py:get_customers_requests.py
#! /usr/bin/python
#
#	get_customers_requests.py
#
#						Dec/16/2021
#
# --------------------------------------------------------------------
import os
import json
import requests
from requests.auth import HTTPBasicAuth
from dotenv import load_dotenv
# --------------------------------------------------------------------
dotenv_path = '.env'
load_dotenv(dotenv_path)
SECRET_KEY = os.environ.get("SECRET_KEY")
#
url="https://api.stripe.com/v1/customers"
rr = requests.get(url, auth=HTTPBasicAuth(SECRET_KEY,''))
#
print(rr)
str_json=rr.text
dict_aa = json.loads(str_json)
#
for uu in dict_aa["data"]:
	print(uu["id"])
#
# --------------------------------------------------------------------
```

実行結果

```text
$ ./get_customers_requests.py 
<Response [200]>
cus_KmrIU4hzWqYt7R
cus_KmoG4k1wiKxSDc
cus_KmnqmJhBAvPGMq
cus_KmnpiRFjm8he97
```

#Python sdk でカスタマーの一覧を取得#

sdk のインストール

```bash
udo pip3 install stripe
```

```py:get_customers_sdk.py
#! /usr/bin/python
#
#	get_customers_sdk.py
#
#					Dec/16/2021
# --------------------------------------------------------------------
import os
import stripe
from dotenv import load_dotenv
# --------------------------------------------------------------------
dotenv_path = '.env'
load_dotenv(dotenv_path)
SECRET_KEY = os.environ.get("SECRET_KEY")

stripe.api_key = SECRET_KEY

aa=stripe.Customer.list(limit=10)
#
print(len(aa["data"]))
#
for uu in aa["data"]:
	print(uu["id"]) 
# --------------------------------------------------------------------
```

実行結果

```text
$ ./get_customers_sdk.py 
4
cus_KmrIU4hzWqYt7R
cus_KmoG4k1wiKxSDc
cus_KmnqmJhBAvPGMq
cus_KmnpiRFjm8he97
```

#Node.js request でカスタマーの一覧を取得#

ライブラリーのインストール

```bash
npm install --save dotenv
npm install --save request
```

```js:get_customers_request.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	get_customers_request.js
//
//						Dec/17/2021
// ---------------------------------------------------------------
async function get_customers_proc(secret_key)
{
	var request = require('request')
	var options = {
	url: 'https://api.stripe.com/v1/customers',
	method: 'GET',
	headers: {
		Authorization: 'Bearer ' + secret_key
		}
	}
 
	request(options, function (error, response, body)
		{
 
		const dict_aa = JSON.parse (body) 

		for(var it in dict_aa["data"])
			{
			const unit_aa = dict_aa["data"][it]
			console.log(unit_aa["id"])
			}

		})
}

// ---------------------------------------------------------------
const dotenv = require('dotenv')

dotenv.config()
const secret_key = `${process.env.SECRET_KEY}`

get_customers_proc(secret_key)
// ---------------------------------------------------------------
```

実行結果

```text
$ ./get_customers_request.js 
cus_KmrIU4hzWqYt7R
cus_KmoG4k1wiKxSDc
cus_KmnqmJhBAvPGMq
cus_KmnpiRFjm8he97
```

#Node.js sdk でカスタマーの一覧を取得#

sdk のインストール

```bash
npm install --save stripe
```

```js:get_customers_sdk.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	get_customers_sdk.js
//
//						Dec/17/2021
// ---------------------------------------------------------------
async function get_customers_proc(secret_key)
{
const stripe = require('stripe')(secret_key)
const customers = await stripe.customers.list({ limit: 10, })

//	console.log(customers["data"])

	for(var it in customers["data"])
		{
		const unit_aa = customers["data"][it]
		console.log(unit_aa["id"])
		}
}

// ---------------------------------------------------------------
const dotenv = require('dotenv')

dotenv.config()
const secret_key = `${process.env.SECRET_KEY}`

get_customers_proc(secret_key)
// ---------------------------------------------------------------
```

実行結果

```text
$ ./get_customers_sdk.js 
cus_KmrIU4hzWqYt7R
cus_KmoG4k1wiKxSDc
cus_KmnqmJhBAvPGMq
cus_KmnpiRFjm8he97
```

#関連情報#
[Go: stripe API 使い方 (List all customers)](https://qiita.com/ekzemplaro/items/d2b2d373a58dcbb86711)
[PHP: stripe API 使い方 (List all customers)](https://qiita.com/ekzemplaro/items/af152de0b35907a2f43f)
[Ruby: stripe API 使い方 (List all customers)](https://qiita.com/ekzemplaro/items/a564f5e27f3071e7bbb8)