第八段
https://www.storyblok.com/docs/Delivery-Api/overview Storyblokのdocsの構造が変わったので、CONTENT DELIVERY APIの項から訳します。その5-7まではチュートリアルの翻訳(途中やり)となってます。
認証(Authentication)とSDK
StoryblokのコンテンツデリバリAPIを呼べるようにするためには、トークンを用いて自分だということを識別する必要があります。このトークンはapp.storyblok.comのStoryblokのダッシュボードにて生成できます。
トークンには3種類あります
-
Preview
: このトークンはコンテンツのdraft及びpublishedへアクセス出来るようにします。 -
Public
: publishedのみへアクセス出来るようにします。 -
Theme
: Cloud Rendering Serviceのテンプレートのアップロードへアクセス出来るようにします。
API
エンドポイントapi.storyblok.com/v1/cdn
から以下のREST-APIが使用できます。
- Stories: コンテンツエントリを受け取る
- Tags: タグを受け取る(タグクラウドなどに使用)
- Links: ストーリーのリンクをマップしたものを受け取る
- Datasource_entries: データソースを受け取る(キーバリューペア)
このエンドポイントは高速に配信できるように作られていて、コンテンツはCDNにキャッシュされています。キャッシュでないコンテンツを受け取りたい場合、URLにバージョンパラメータを渡す必要があります。このパラメータはcache_version
というもので、通常はタイムスタンプを持ちます。(SDKは自動でバージョンのタイムスタンプを更新します。)
SDK
開発はSDKを使えばより簡単になります。以下のSDKは現在利用可能なものです。今後も追加されます。
JavaScript
<body>
<script src="https://app.storyblok.com/f/storyblok-latest.js?t=hZ9t5mLywGe41ragtcSLgwtt">
</script>
<script>
// Get the content of the current page
storyblok.get({slug: 'home', version: 'draft'}, function(data) {
console.log(data.story)
})
// Get multiple content entries from a folder called "news"
storyblok.getAll({starts_with: 'story', version: 'draft'}, function(data) {
console.log(data.stories)
})
</script>
</body>
Node.js
// Install by $ npm install storyblok-js-client --save
import StoryblokClient from 'storyblok-js-client'
var Storyblok = new StoryblokClient({
accessToken: '{{preview_token}}' // Your preview token
})
// Get the content of the current page
Storyblok
.get('cdn/stories/home', {
version: 'draft'
})
.then((response) => {
console.log(response)
})
// Get multiple content entries from a folder called "news"
Storyblok
.get('cdn/stories', {
starts_with: 'news',
version: 'draft'
})
.then((response) => {
console.log(response)
})
Ruby
client = new Storyblok::Client(token: '{{preview_token}}') # Your preview token
# Get the content of the current page
p client.story('home')
# Get multiple content entries from a folder called "news"
p client.stories({
:starts_with => 'news'
})
# More on https://github.com/storyblok/storyblok-ruby
PHP
require 'vendor/autoload.php';
$client = new \Storyblok\Client('{{preview_token}}'); // Your preview token{%- raw -%}
// Get the content of the current page
$client->getStoryBySlug('home');
$data = $client->getBody();
// Get multiple content entries from a folder called "news"
$client->getStories(
array(
'starts_with' => 'news'
)
);
$data = $client->getStoryContent();{% endraw %}
# More on https://github.com/storyblok/php-client