Api Resourceのロゴ

Api Resource

組織
api-platform
api-resource

Creates or modifies API Platform resources with DTOs and Object Mapper. Use whenever the user wants to add an API endpoint, expose an entity or any data over HTTP, create or reshape a resource, define input/output DTOs, configure nested sub-resources with uriVariables, or map entities/documents to API representations — even if they just say 'add an endpoint for X' or 'expose X in the API'.

概要

公開者api-platform
リポジトリskillset
スキル名api-resource
スター
29
フォーク
1
同梱ファイル
指示のみ
リンク
  • Markdown形式の指示

    モデルが必要に応じて読み込むSKILL.mdファイルです。関連するリクエストがあったときだけトークンを消費します。

  • どのLLMでも利用可能

    AIスキルはプロバイダー固有のコードではなく、通常のMarkdownなので、GPT、Claude、Gemini、Grok、ローカルモデルで利用できます。

  • 指示だけで完結

    モデルに必要なものはすべて指示に含まれているため、別途同期するファイルはありません。

  • オープンソース

    api-platformがGitHubで公開しています。インストールする前にソースを確認してください。

インストール

AIスキルApi ResourceをTypingMindにインストールすれば、どのLLMでも利用できます。SKILL.mdを読み取れる別のAIエージェントに追加することもできます。

1

TypingMindにインストール

TypingMindはGitHubフォルダから直接スキルをインストールします。SKILL.mdを読み取り、リソースファイルをまとめてローカルに保存します。

  1. アプリを開き、「プラグイン」→「スキル」に移動します。
  2. 「GitHubからインストール」を選択します。
  3. 以下のスキルフォルダのURLを貼り付けて確定します。
    https://github.com/api-platform/skillset/tree/main/skills/api-resource
  4. スキルを利用したいチャットごとに、そのスキルを有効にしてください。
「プラグイン」→「スキル」→「スキルを追加」→「GitHubのURLから」を選択し、フォルダのURLを貼り付けて「続行」を押します。
2

別のAIエージェントにインストール

Agent Skills形式を読み取れるAIエージェントなら、このスキルを利用できます。フォルダをそのAIエージェントのスキルディレクトリにコピーしてください。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/api-platform/skillset.git /tmp/skillset
mkdir -p .claude/skills
cp -r /tmp/skillset/skills/api-resource .claude/skills/api-resource
コピー後、新しいスキルを読み込むためにClaude Codeを再起動してください。

TypingMindで使用

TypingMindのチャットでApi Resourceを有効にするだけで、あとはモデルが対応します。名前と説明がシステムプロンプトに含まれ、関連するリクエストがあると、モデルが指示の全文を自動で読み込みます。手動で呼び出す必要はなく、実際に使用されるまでトークンを消費しません。

関連するリクエストがあると、モデルが自動的にApi Resourceを読み込みます。

どのAIモデルでも利用可能

AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、Api Resourceは作成時に想定されたモデルに縛られません。TypingMindに一度インストールすれば、ご自身のAPIキーを使って、GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama、またはご自身で実行するローカルモデルで利用できます。

  • 必要なときだけ読み込み

    システムプロンプトには名前と説明だけが含まれます。関連するリクエストが初めてあったときに指示を取得するため、未使用のスキルにはコストがかかりません。

  • チャットの途中でモデルを切り替え

    スキルはコードではなく指示なので、モデルを切り替えてもそのまま使えます。次のモデルも同じSKILL.mdを読み込みます。

スキルの指示

モデルが読み込むSKILL.mdの内容です。スキルはモデルが従う指示なので、インストールする前に確認してください。

Creating API Platform Resources

Design-First Principle

An API Platform resource is built from three concerns (see https://api-platform.com/docs/core/design/):

  1. Resource declaration — a plain PHP object marked #[ApiResource] describing the public shape. Single source of truth for Hydra, OpenAPI and GraphQL.
  2. Data retrieval — a state provider hydrates that object (Get, GetCollection).
  3. Data persistence — a state processor writes it (Post, Put, Patch, Delete).

Design the public shape first; the resource class doesn't have to be a Doctrine entity. How you wire it to persistence is a separate decision:

ApproachWhenHookup
Entity as ResourcePrototyping, plain CRUD, internal model == public shape#[ApiResource] on the entity; built-in Doctrine provider/processor (zero wiring)
DTO with Object MapperDecoupled public shape over a Doctrine entity/document — whether fields match 1:1 or need renames/transformsstateOptions: new Options(entityClass:/documentClass:) and #[Map] on the DTO
Input DTOs per OperationWrite model differs from read model (stricter create/update payloads)per-operation input: + processor
Custom Provider/ProcessorNon-CRUD, external data, complex domain logic, CQRShand-written ProviderInterface/ProcessorInterface (see state-provider/state-processor)

Rule of thumb: entity-as-resource is convenient but couples your public contract to your schema — fine for prototypes, probably not for large or non-CRUD systems. Decouple with a DTO as soon as the two shapes diverge.

Entity as Resource (Simplest)

Mark the entity itself — built-in Doctrine providers/processors handle everything. No provider, processor, or mapping to write:

php
use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ApiResource]
class Book
{
    #[ORM\Id, ORM\GeneratedValue, ORM\Column]
    public ?int $id = null;

    #[ORM\Column]
    public string $title = '';
}

Use this for prototypes and straight CRUD. Migrate to a DTO (strategies below) once the API shape must differ from the schema.

DTO with Object Mapper (Recommended)

One mechanism, not two: API Platform's ObjectMapperProvider / ObjectMapperProcessor activate only when both are present — stateOptions naming the entityClass: (or documentClass:) and a #[Map] attribute on the DTO (and on the input entity for writes). On read it runs map($entity, $resourceClass); on write map($inputDto, $entityClass) then persists via the Doctrine processor. There is no Object-Mapper mode without stateOptions.

Fields that line up by name map automatically; add #[Map(source: …)] to rename or transform: to convert. The same config covers both the trivial 1:1 case and arbitrary renames/transforms:

php
<?php
namespace App\ApiResource;

use ApiPlatform\Doctrine\Odm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use App\Document\Order as DocumentOrder;
use App\Transformer\CustomerTransformer;
use Symfony\Component\ObjectMapper\Attribute\Map;

#[ApiResource(
    operations: [
        new Get(stateOptions: new Options(documentClass: DocumentOrder::class)),
        new GetCollection(stateOptions: new Options(documentClass: DocumentOrder::class)),
    ]
)]
#[Map(target: DocumentOrder::class)]
final class Order
{
    public string $id;

    // Simple 1:1 field mapping (automatic when names match)
    public string $status;

    // Map from a different source field
    #[Map(source: 'customerName')]
    public string $buyer;

    // Transform with a custom callable
    #[Map(source: 'rawData', transform: new CustomerTransformer())]
    public CustomerDto $customer;
}

Custom Transformers

Implement TransformCallableInterface for complex mappings:

php
<?php
namespace App\Transformer;

use Symfony\Component\DependencyInjection\Attribute\Exclude;
use Symfony\Component\ObjectMapper\TransformCallableInterface;

#[Exclude]
final class CustomerTransformer implements TransformCallableInterface
{
    public function __construct(private readonly string $field = 'name') {}

    public function __invoke(mixed $value, object $source, ?object $target): mixed
    {
        // $value = source field value, $source = full source object
        $dto = new CustomerDto();
        $dto->name = $value[$this->field] ?? '';
        return $dto;
    }
}

Use #[Exclude] so Symfony's container doesn't try to autowire transformer constructor args.

Input DTOs per Operation

When the write model differs from the read model, give individual operations their own input: DTO and processor (the resource class stays the read model):

php
#[ApiResource(
    operations: [
        new Post(input: CreateOrderInput::class, processor: OrderCreateProcessor::class),
        new Patch(input: UpdateOrderInput::class, processor: OrderUpdateProcessor::class),
    ]
)]
class Order { /* read model */ }

Nested Sub-Resources with uriVariables

For resources nested under parents (e.g., /accounts/{accountId}/mailboxes/{mailboxId}/messages):

php
#[ApiResource(
    operations: [
        new Get(
            uriTemplate: '/accounts/{accountId}/mailboxes/{mailboxId}/messages/{id}',
            uriVariables: ['accountId', 'mailboxId', 'id'],
            stateOptions: new Options(
                handleLinks: MessageLinkHandler::class,
                documentClass: Message::class,
            )
        ),
        new GetCollection(
            uriTemplate: '/accounts/{accountId}/mailboxes/{mailboxId}/messages',
            uriVariables: ['accountId', 'mailboxId'],
            stateOptions: new Options(
                handleLinks: MessageLinkHandler::class,
                documentClass: Message::class,
            ),
            itemUriTemplate: '/accounts/{accountId}/mailboxes/{mailboxId}/messages/{id}',
        ),
    ]
)]

The handleLinks class validates parent ownership and applies security filters. See the securing-collections skill for implementation details.

Hidden IRI fields for URI generation:

php
#[ApiProperty(readable: false, writable: false)]
#[Map(source: 'account', transform: new DocumentIdTransformer())]
public string $accountId;

Custom Named Operations

Add non-CRUD actions on the same resource:

php
new Put(
    uriTemplate: '/orders/{id}/cancel',
    input: CancelOrderInput::class,
    processor: OrderCancelProcessor::class,
    name: '_api_order_cancel',
),
new Get(
    write: true, // triggers processor on GET
    uriTemplate: '/orders/{id}/download',
    processor: OrderDownloadProcessor::class,
    name: '_api_order_download',
),

Use write: true on Get operations that need a processor (e.g., file downloads): a Get's write phase is off by default, so the processor never fires until you flip the flag. See operations → Read & write phases for the read/write lifecycle.

Backed Enums

As a property

A backed enum property serializes to its ->value automatically:

php
#[ApiResource]
class Person
{
    public GenderType $genderType; // backed enum → {"genderType": "female"}
}

As a resource

Expose a backed enum as a read-only resource so clients can discover allowed values:

php
use ApiPlatform\Metadata\ApiResource;

#[ApiResource]
enum AvailabilityStatus: string
{
    case InStock = 'InStock';
    case OutOfStock = 'OutOfStock';
}

GET /availability_statuses lists all cases; GET /availability_statuses/InStock returns one.

High-Precision Numbers

For monetary/scientific values that must avoid float drift, type a property as \BcMath\Number (PHP 8.4 native, requires ext-bcmath). It serializes as a string to preserve precision:

php
class Invoice
{
    public ?\BcMath\Number $total; // → "300.55"
}

Laravel (Eloquent)

The design-first split, DTOs, Object Mapper, per-operation input:, nested uriVariables/handleLinks, custom operations and backed-enum resources all work on Laravel. Deltas:

  • Model as resource: #[ApiResource] on a class extending Illuminate\Database\Eloquent\Model; the Eloquent provider/processor handle CRUD with zero wiring.
  • Eloquent Options (ApiPlatform\Laravel\Eloquent\State\Options) uses modelClass: (not entityClass:/documentClass:); it carries modelClass + handleLinks only — no repositoryMethod.
  • Object Mapper is supported: pair stateOptions: new Options(modelClass: ProductModel::class) with #[Map(source: ProductModel::class)] on the DTO; per-property #[Map] and TransformCallableInterface are identical.
  • Properties: Eloquent models have no typed properties, so declare #[ApiProperty] at the class level with property: (#[ApiProperty(property: 'title', identifier: true)]). DTO/ApiResource classes use property-level attributes. \BcMath\Number and backed enums behave the same.

Checklist

When creating a new resource:

  • Create the DTO class in src/ApiResource/
  • Add #[ApiResource] with operations
  • Define provider for read operations (or use stateOptions)
  • Define processor for write operations
  • Add validation constraints to input DTOs
  • For nested resources: configure uriVariables and handleLinks
  • Add #[Map] attributes for entity/document mapping
  • Add #[ApiProperty] for OpenAPI documentation

よくある質問

AIスキル「Api Resource」では何ができますか?

Creates or modifies API Platform resources with DTOs and Object Mapper. Use whenever the user wants to add an API endpoint, expose an entity or any data over HTTP, create or reshape a resource, define input/output DTOs, configure nested sub-resources with uriVariables, or map entities/documents to API representations — even if they just say 'add an endpoint for X' or 'expose X in the API'.

TypingMindでApi Resourceを使うメリットは何ですか?

一度インストールすれば、どのモデルでも使えるからです。Api Resourceはプロバイダー固有のコードではなく、通常のMarkdownで記述されているため、同じスキルをGPT-5、Claude、Gemini、Grok、ローカルモデルで利用できます。チャットの途中でモデルを切り替えても、そのまま使い続けられます。TypingMindは自分のAPIキーで動作するため、シート単位のサブスクリプションではなく、プロバイダーに直接料金を支払います。スキルとチャットは自分のストレージに保持されます。

TypingMindにApi Resourceをインストールするにはどうすればよいですか?

TypingMindで「プラグイン」→「スキル」→「GitHubからインストール」を開き、https://github.com/api-platform/skillset/tree/main/skills/api-resourceを貼り付けてください。TypingMindがSKILL.mdを読み込み、チャットごとに有効にできるスキルとしてインストールします。

Api Resourceを使えるAIモデルはどれですか?

TypingMindに接続したどのモデルでも利用できます。AIスキルはプロバイダー固有のコードではなく、通常のMarkdownで書かれた指示です。そのため、GPT、Claude、Gemini、Grok、ローカルモデルのいずれも、リクエストが合致したときにこのスキルを読み込めます。

Api Resourceは何個のAIモデルで使えますか?

数に制限はありません。スキルに対応したモデルであれば、GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llamaなど、どのモデルでもApi Resourceを利用できます。すべてTypingMind上で、自分のAPIキーを使って利用できます。

AIスキル「Api Resource」は無料ですか?

api-platformがGitHubで公開しています。ライセンス条件はリポジトリでご確認ください。支払いが必要なのは、利用したトークンに対する、ご自身が契約するAIプロバイダーへの料金だけです。

AIスキルとは何ですか?

AIスキルは、特定のタスクの実行方法をAIモデルに教える、再利用可能な指示のセットです。オープンなAgent Skills形式に従い、名前と説明を含むSKILL.mdファイルに加え、モデルが必要とするスクリプト、テンプレート、参照ファイルで構成されます。モデルはリクエストがスキルに合致したときだけ指示を読み込むため、インストール済みのスキルも、使うまでは費用がかかりません。

AIスキルは、プラグインやMCPサーバーとどう違いますか?

プラグインやMCPサーバーは、モデルが呼び出せる新しいツールを提供します。ツールとは、どこかで実行され、結果を返すコードです。一方、AIスキルは、タスクへの取り組み方、従うべき手順、望ましい出力の例といった知識や進め方をモデルに伝えます。スキルは通常のMarkdownで記述されているため、サーバー、APIキー、実行環境は不要で、どのモデルでも利用できます。

すべて見る

今すぐ自分専用のAIワークスペースを作成

ニュースレターに登録すると、新機能や今後のプレゼント企画のお知らせが届きます👇