|
| 1 | +## How to create REST API Endpoints |
| 2 | + |
| 3 | + ``Thinking Headless, make REST API`` |
| 4 | + |
| 5 | +We should create magento 2 extension to make REST API |
| 6 | + |
| 7 | +### Define REST API Endpoints |
| 8 | + |
| 9 | +Before create API Endpoints, should think there data which module will delivery to client, security for data, permission for data |
| 10 | + |
| 11 | +Define API endpoint in file ``/etc/webapi.xml`` |
| 12 | + |
| 13 | +API Endpoint name should have name as this: ``V1/[end-point-name]/[action]/[params]`` |
| 14 | +end-point-name we can use module vendor prefix. It will use for all function relate this endpoint. |
| 15 | +action we can set detail of endpoint function. Example : ``resetPassword`` |
| 16 | + |
| 17 | +Example: Customer Account Management API Endpoints |
| 18 | + |
| 19 | +``/V1/customers`` - POST |
| 20 | +``/V1/customers/{customerId}/password/resetLinkToken/{resetPasswordLinkToken}`` - GET |
| 21 | + |
| 22 | +{customerId}, {resetPasswordLinkToken} are params |
| 23 | + |
| 24 | +``/V1/customers/password`` - PUT |
| 25 | + |
| 26 | +``/V1/customers/resetPassword`` - POST |
| 27 | + |
| 28 | +``/V1/customers/isEmailAvailable`` - POST |
| 29 | + |
| 30 | +at here we have "customers" is end-point-name |
| 31 | + |
| 32 | +### Security for data |
| 33 | + |
| 34 | +Magento 2 support Authentication for REST API. There are four account types (in order of descending permissions): `Admin`, `Integration`, `Customer` and `Guest`. |
| 35 | + |
| 36 | +`Admin` users can access anything. |
| 37 | + |
| 38 | +`Integration` users are only used by the OAuth authentication system. They are intended to be used in situations where a module needs API access to an installation, but admin access has not been granted. Their access to resources is limited to their custom ACL role, `self` or `anonymous`. Specific ACL roles need to be created for integration users. |
| 39 | + |
| 40 | +`Customer` users can only access resources with a type of `self` or `anonymous`, i.e. Only the data for that specific customer. |
| 41 | + |
| 42 | +`Guest` users can only access resources with a type of `anonymous`. If Magento cannot authenticate an API consumer, they default to the `Guest` type. |
| 43 | + |
| 44 | + |
| 45 | +Because REST API can return more data from database. So, we should think some sensitive data should not response or need permission to access them. |
| 46 | + |
| 47 | +#### Example: |
| 48 | + |
| 49 | +get current logged in customer data, REST API require access with permission resource = self |
0 commit comments