Use your APIs
#
Use Your APIsEvery application created with Amplication is generated with two types of APIs: REST, and GraphQL.
In this article you will learn how to connect, authenticate, and consume these APIs.
#
AuthenticationWhile we are working to add more authentication providers, currently your server is secured with Basic HTTP Authentication.
To send a request to the API you must provide a Basic HTTP authentication header in the form of Authorization: Basic [credentials], where credentials is the Base64 encoding of a string "username:password".
By default, your app comes with one user with the username "admin" and password "admin". You can use the following header to authenticate with the default user.
tip
You can use a tool to create the header. There are several generators available online.
#
REST APIThe REST API is available at /api at the root of your application. When you navigate directly to /api you will see the swagger documentation of you API with a list of all resources and actions.
For development and testing purposes, you can use the swagger UI to execute requests against the API. First, click on the "Authorize" button and enter the username and password, it will add the authorization header automatically.
The REST API provide methods on all your data model. For each model, you can find five endpoints. For example, on the User model you will find the following endpoints:
GET /api/users
- to get a list of usersGET /api/users/:id
- to get a single user by its IDPOST /api/users
- to create a new userPATCH /api/users/:id
- to update an existing user by its IDDELETE /api/users/:id
- to delete a user by its ID
#
GraphQL APIThe GraphQL API is available at /graphql at the root of your application.
When you navigate directly to /graphql you will see the GraphQL Playground provided by Apollo Server.
For development and testing purposes, you can use the GraphQL Playground to execute queries and mutations against the API. First, click on the "HTTP HEADERS" tab at the bottom of the screen and add the authorization header in the following format:
The GraphQL API provide queries and mutations on all your data model. For each model, you can find 2 queries and 3 mutations. \ For example, for the User model you will find the following:
#
Queriesusers
- to get a list of usersuser
- to get a single user by its ID
#
MutationscreateUser
- to create a new userupdateUser
- to update an existing user by its IDdeleteUser
- to delete a user by its ID
Click on the DOCS tab on the right hand side to view a full documentation of all queries and mutations.