0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonでkintoneのレコードを一括取得・更新・追加する方法

Posted at

Pythonでkintoneのレコードを一括取得・更新・追加する方法

こんにちは、今回はPythonを使ってkintoneのデータを操作する方法をご紹介します!

前回はレコードを取得する方法を解説しましたが、今回はそれに加えて、レコードの更新と追加もPythonでできる方法をまとめて紹介します。


🔧 事前準備

kintoneのデータをPythonから操作するには、以下の情報が必要です。
下記のリンクを参考にしてください!
https://qiita.com/reo_python_spy/items/45da1bb039494952de02

✏️ レコードの更新(PUT)

既存レコードのフィールドを変更したいときは、record.json に対して PUT メソッドを使います。

def update_kintone_record(record_id):
    url = f'{BASE_URL}/k/v1/record.json'
    headers = {
        'X-Cybozu-API-Token': API_TOKEN,
        'Content-Type': 'application/json'
    }
    field_data = {
        "顧客名": {"value": "株式会社アップデート"},
        "電話番号": {"value": "03-0000-0000"}
    }
    update_data = {
        "app": APP_ID,
        "id": record_id,
        "record": field_data
    }
    update_response = requests.put(url, headers=headers, json=update_data)
    return update_response.json()

使用例:

response = update_kintone_record(1234)
print(response)

➕ レコードの追加(POST)

新しいレコードを追加するには、record.json に対して POST メソッドを使います。

def add_kintone_record():
    url = f'{BASE_URL}/k/v1/record.json'
    headers = {
        'X-Cybozu-API-Token': API_TOKEN,
        'Content-Type': 'application/json'
    }
    field_data = {
        "顧客名": {"value": "株式会社アップデート"},
        "電話番号": {"value": "03-0000-0000"}
    }
    add_data = {
        "app": APP_ID,
        "record": field_data
    }
    add_response = requests.post(url, headers=headers, json=add_data)
    return add_response.json()

使用例:

response = add_kintone_record()
print(response)

✅ まとめ

  • レコード更新PUT メソッドでID指定してフィールドを更新
  • レコード追加POST メソッドで新規レコードを作成

📢 最後に

kintoneのAPIはシンプルですが、繰り返し処理やデータ整形を理解することで、業務自動化やバックエンド連携が可能になります!

この内容が「これからkintone + Pythonを使ってみたい」という方の助けになれば嬉しいです 🙌

Qiitaへの「いいね」やコメントも励みになります。ありがとうございました!

0
1
1

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?