0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DatabuttonでFirestoreに接続してみた!ノーコードでAPIアプリを構築

Last updated at Posted at 2025-05-01

DatabuttonでFirestoreに接続してみた!ノーコードでAPIアプリを構築

Databuttonは、Pythonベースで簡単にアプリを作れるノーコード/ローコードプラットフォームです。この記事では、Firestore(Google CloudのNoSQL DB)に接続し、API経由でデータを読み書きできるシンプルなアプリの作成方法を紹介します。

🎯 この記事でできること

  • Firestoreに接続するDatabuttonアプリの作成
  • Firestoreからのデータ取得・書き込み
  • Databutton上でのAPIエンドポイントの作成

🧩 必要な準備

1. Firestoreのプロジェクト作成

Google Cloud Consoleで以下を設定します:

  • プロジェクト作成
  • Firestoreを有効化(ネイティブモード)
  • サービスアカウント作成(JSONキーをダウンロード)

2. Databuttonで新しいアプリを作成


🔧 コアコード(接続・取得・登録・API定義)

from google.cloud import firestore
import json
from databutton import api

# Firestore接続
with open("service_account.json") as f:
    service_account_info = json.load(f)
db = firestore.Client.from_service_account_info(service_account_info)

# データ取得
def get_data():
    docs = db.collection("users").stream()
    return [doc.to_dict() for doc in docs]

# データ追加
def add_user(name, email):
    doc_ref = db.collection("users").document()
    doc_ref.set({"name": name, "email": email})
    return {"status": "ok"}

# API定義
@api
def get_users():
    return get_data()

@api
def create_user(name, email):
    return add_user(name, email)

✅ 実行例

  • GET /get_users → ユーザー一覧を取得
  • POST /create_user(JSONボディにnameemail)→ 新しいユーザーを登録

Databuttonの「API Tester」や「Playground」から実行して確認できます。


🧭 まとめ

Databuttonを使えば、Google Cloud Firestoreとの連携も非常に簡単です。PythonベースでAPIを公開でき、UIや管理画面も統合可能なので、スタートアップや個人開発にも最適です。


🔗 参考リンク


🔥 気に入ったらLGTM・コメントお願いします!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?