Netlify REST APIのapi/v1/sitesにいろいろなGETリクエストを送ってみる
仕事で使う用事があったので、簡単な覚え書き。 Personal Access Tokensの取得 REST APIをコールするには「Personal Access Token」が必要です。https://app.netl […]
広告ここから
広告ここまで
目次
仕事で使う用事があったので、簡単な覚え書き。
Personal Access Tokensの取得
REST APIをコールするには「Personal Access Token」が必要です。https://app.netlify.com/applications/personalから作成しましょう。
作成したTokenは、後で見直すことはできない様子なので要注意です。
忘れたり紛失した場合は再発行しましょう。
curlでリクエストする
ひとまずGETでリクエスト投げてみましょう。
GET api/v1/sites
$ curl https://api.netlify.com/api/v1/sites -H "Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN"
リクエストすると、Netlifyに作成しているサイトの情報がJSONで返ってきます。
[ { "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "site_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "plan": "nf_pages", "plan_data": { "title": "Netlify Pages", "asset_acceleration": false, "form_processing": false, "cdn_propagation": "partial", "domain_aliases": false, "secure_site": false, "prerendering": false, "proxying": false, "ssl": "letsencrypt", "rate_cents": 0, "yearly_rate_cents": 0, "cdn_network": "free_cdn_network", "id": "nf_pages" }, "ssl_plan": null, "premium": false, "claimed": true, "name": "XXXXXXX-XXXXXXXX-99999", "custom_domain": null, "domain_aliases": [], "password": null, "notification_email": null, "url": "https://XXXXXXX-XXXXXXXX-99999.netlify.com", "admin_url": "https://app.netlify.com/sites/XXXXXXX-XXXXXXXX-99999", "deploy_id": "99999999999999999999999", "build_id": "", "deploy_url": "https://99999999999999999999999.XXXXXXX-XXXXXXXX-99999.netlify.com", "state": "current", "screenshot_url": "https://353a23c500dde3b2ad58-c49fe7e7355d384845270f4a7a0a7aa1.ssl.cf2.rackcdn.com/99999999999999999999999/screenshot.png", "created_at": "2016-12-16T07:34:45Z", "updated_at": "2016-12-16T07:34:52Z", "user_id": "XXXXXXXXXXXXXXXXXXXXXXXX", "error_message": null, "ssl": false, "force_ssl": null, "ssl_status": null, "max_domain_aliases": 0, "build_settings": { "repo_type": "git", "repo_url": "https://github.com/hideokamoto/example", "repo_branch": "gh-pages", "cmd": null, "dir": null, "env": {}, "created_at": "2016-12-16T07:34:45Z", "updated_at": "2016-12-16T07:34:45Z" }, "processing_settings": { "css": { "bundle": true, "minify": true }, "js": { "bundle": true, "minify": true }, "images": { "optimize": true }, "html": { "pretty_urls": true }, "skip": true }, "prerender": null, "prerender_headers": null, "deploy_hook": "https://api.netlify.com/hooks/github", "published_deploy": { "id": "99999999999999999999999", "site_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "build_id": "58539915d6865d39b3f0ac6f", "state": "ready", "name": "XXXXXXX-XXXXXXXX-99999", "url": "https://XXXXXXX-XXXXXXXX-99999.netlify.com", "admin_url": "https://app.netlify.com/sites/XXXXXXX-XXXXXXXX-99999", "deploy_url": "https://99999999999999999999999.XXXXXXX-XXXXXXXX-99999.netlify.com", "deploy_ssl_url": "https://99999999999999999999999--XXXXXXX-XXXXXXXX-99999.netlify.com", "screenshot_url": "https://353a23c500dde3b2ad58-c49fe7e7355d384845270f4a7a0a7aa1.ssl.cf2.rackcdn.com/99999999999999999999999/screenshot.png", "created_at": "2016-12-16T07:34:45Z", "updated_at": "2016-12-16T07:34:52Z", "user_id": "XXXXXXXXXXXXXXXXXXXXXXXX", "error_message": null, "required": [], "commit_ref": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "review_id": null, "branch": "gh-pages", "commit_url": "https://github.com/hideokamoto/example/commit/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "skipped": null, "locked": null, "log_access_attributes": { "type": "firebase", "url": "https://netlify.firebaseio.com/builds/XXXXXXXXXXXXXXXXXX/log" }, "title": null, "review_url": null, "published_at": null }, "managed_dns": null } ]
GET api/v1/sites/{site_id}
$ curl https://api.netlify.com/api/v1/sites/{site_id} -H "Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN"
こちらはNetlifyにあるサイトの情報をID指定で取得するものです。
戻り値はapi/v1/sites
とほぼ同じです。
ちなみに、pi/v1/sites/XXXX-XXXX-99999.netlify.com
のようにドメインでも検索できます。
GET api/v1/sites/{site_id}/files
filesを追加すると、該当するsite_idでアップロードされているファイルの一覧が見れます。
$ curl https://api.netlify.com/api/v1/sites/{site_id}/files -H "Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN" [ { "id": "/license", "path": "/license", "sha": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "mime_type": "text/plain", "size": 11857, "site_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "deploy_id": "XXXXXXXXXXXXXXXXXXXXXXXX" }, { "id": "/aws-sdk-2.4.7.min.js", "path": "/aws-sdk-2.4.7.min.js", "sha": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "mime_type": "application/javascript", "size": 460645, "site_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "deploy_id": "XXXXXXXXXXXXXXXXXXXXXXXX" }, { "id": "/index.html", "path": "/index.html", "sha": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "mime_type": "text/html", "size": 11684, "site_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "deploy_id": "XXXXXXXXXXXXXXXXXXXXXXXX" }, ]
GET api/v1/sites/{site_id}/files/{path-to-file}
さらにファイルパスを追加すると、そのファイルの情報だけを引き出すことができます。
$ curl https://api.netlify.com/api/v1/sites/{site_id}/files/index.html -H "Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN" { "id": "/index.html", "path": "/index.html", "sha": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "mime_type": "text/html", "size": 11684, "site_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", "deploy_id": "XXXXXXXXXXXXXXXXXXXXXXXX" }
おわりに
サイトの情報をAPIからガンガン引っ張ってこれるのは便利ですね。
今回はとりあえずGETリクエスト限定で試してみましたが、POSTでデプロイやリストアもできる様子なのでいろいろ試していきます。