Amplify CLI API Category Plugin
raw JSON →The `@aws-amplify/amplify-category-api` package is an internal plugin for the AWS Amplify Command Line Interface (CLI). It empowers developers to create, configure, and manage both GraphQL (AWS AppSync) and REST (Amazon API Gateway) API resources within their Amplify projects. It's not a library for direct programmatic consumption but rather a core component of the `amplify` CLI toolchain, which currently operates at major version `14.x.x` for the CLI itself. This specific plugin's latest stable version is `5.15.3`. The Amplify CLI maintains a frequent release cadence, often with minor updates and bug fixes for its various categories. Key differentiators include its declarative API modeling with GraphQL Schema Definition Language (SDL) and automatic CloudFormation generation, alongside capabilities for local mocking and team environment management.
Common errors
error GraphQL schema validation failed ↓
amplify/backend/api/<api-name>/schema.graphql for errors. Use an IDE with GraphQL syntax highlighting and refer to Amplify's GraphQL Transformer documentation for correct directive usage. Running amplify api gql-compile can help identify specific issues. error An error occurred during the push operation: Your GraphQL Schema is using "@key" directive from an older version of the GraphQL Transformer. ↓
amplify migrate api. Update @key directives to @primaryKey or @index as appropriate, and review @auth rules for the new 'deny-by-default' behavior. error Stack named 'amplify-<projectname>-api-<envname>' already exists ↓
ROLLBACK_COMPLETE or UPDATE_ROLLBACK_FAILED state, you may need to manually delete it. If you are confident, amplify push --force can sometimes resolve minor inconsistencies, but this should be used with caution. error Error: There are no categories configured in the project. Use 'amplify add <category>' to add a category. ↓
amplify init if this is a new project, or amplify pull if you are cloning an existing project. Then, use amplify add api to configure your API. Warnings
breaking Upgrading the Amplify CLI (especially across major versions) often introduces breaking changes, particularly concerning the GraphQL Transformer (e.g., v1 to v2 migration). Directives like `@key` were replaced by `@primaryKey` and `@index`, and authorization rules (`@auth`) shifted to a 'deny-by-default' approach requiring explicit configuration. ↓
gotcha Manual modifications to Amplify-provisioned API resources directly in the AWS console (e.g., AppSync resolvers, DynamoDB tables) can lead to CloudFormation drift. Subsequent `amplify push` operations might overwrite these manual changes or fail due to state inconsistencies. ↓
gotcha Frequent `amplify push` operations during active API development, especially with complex GraphQL schemas, can be slow. This can hinder rapid iteration due to the time required for CloudFormation deployments. ↓
deprecated The `amplify rebuild api` command, used in GraphQL Transformer v1, is deprecated and no longer reliably works with newer transformer versions. ↓
Install
npm install amplify-category-api yarn add amplify-category-api pnpm add amplify-category-api Imports
- No direct programmatic imports for application code wrong
import { addApi } from '@aws-amplify/amplify-category-api'; const apiCategory = require('@aws-amplify/amplify-category-api');correct
Quickstart
npx create-react-app my-amplify-app
cd my-amplify-app
amplify init # Follow prompts to initialize a new Amplify project
# Add a GraphQL API
amplify add api
# Choose 'GraphQL'
# Choose 'Authorize and configure with AWS IAM (recommended for multi-user apps)'
# Choose 'Single object with fields (e.g., "Todo")' or 'Blank schema'
# Edit schema.graphql if desired (e.g., add more @model types)
# Deploy the API to the cloud
amplify push # Confirm changes and deploy