Want to connect your Magento 2 store with another application or make store data available to a custom integration? Creating REST API routes is a great place to start. With the right configuration, you can define how requests reach your code and what actions your API makes available.
This guide walks you through the essentials of creating Magento 2 REST API routes, from defining routes in webapi.xml to connecting them with service contracts and setting access permissions. Follow along to build and test an API endpoint with confidence.
Understand Magento 2 REST API routes and their requirements
Before defining an endpoint, it helps to picture what happens after a client sends a request: Magento must identify the route, check whether the caller is allowed to use it, and pass the call to the right code. Understanding those handoffs will make the configuration steps ahead easier to follow—and help you avoid routes that resolve but cannot run successfully.
A route is more than a URL: it connects an HTTP method and path to a service operation, while also declaring who can access it. The next two areas explain that connection and the groundwork your module needs.
How Magento 2 REST API routes connect requests to services
Magento reads route declarations from a module’s etc/webapi.xml file. Each declaration maps an HTTP verb and URL path—such as GET /V1/example/items—to a service contract interface and method. When a matching request arrives, Magento dispatches it to that operation rather than requiring the route itself to contain business logic.
This separation keeps API behavior reusable and easier to maintain. The service contract should define the operation, while its implementation handles the work; dependency injection connects the interface to that implementation. A route also names an ACL resource, so authorization is checked as part of handling the request.
- HTTP method and URL: determine which route matches the request.
- Service interface and method: identify the operation Magento should call.
- ACL resource: controls which authenticated users or integrations may access it.
Check module structure, permissions, and authentication before you begin
Once the request flow is clear, check that your module can supply every piece Magento expects. A typical module needs its registration and module configuration files, the service contract and implementation, and the route declaration; if you add a new permission, include its definition in etc/acl.xml as well.
Choose access intentionally. Routes for customer or administrative operations generally require an ACL resource, while a route intended for unauthenticated access can use the appropriate anonymous resource. For protected routes, clients must authenticate—commonly with a bearer token—and the token’s role must have permission for the resource.
Before testing, confirm that:
- The module is enabled and its XML files are valid.
- The service method and parameter types match the request you plan to send.
- The selected ACL permission is assigned to the intended role or integration.
- Your test request uses the correct store-scoped REST URL and credentials, if required.
These checks catch common setup issues early, so you can focus on implementing and testing the endpoint rather than debugging a missing interface or denied permission.
Create and configure Magento 2 REST API routes
With the request flow mapped out, it’s time to connect the pieces that make an endpoint callable. You’ll create a service contract, point a route to it, and ensure Magento can resolve and authorize the request.
Start with the code Magento will call: a service interface gives the operation a stable contract, while module registration makes its files available to the application.
Register your module and define its API service interface
Confirm your module has registration.php and etc/module.xml, then add an API interface under a service-contract namespace such as Api. For example, VendorCatalogApiItemLookupInterface could declare a getById method. Use PHPDoc to specify parameter and return types, particularly when returning data objects or arrays, so Magento can interpret the service contract correctly.
Place the implementation in a Model or other suitable class, but keep business logic out of the route configuration. A useful contract might expose getById(int $id) and return an item data interface. This separation lets other Magento code reuse the operation without making an HTTP request.
Once the interface and implementation exist, the route declaration can connect a specific HTTP request to that operation. The example below exposes a protected GET endpoint for an item lookup.
Add route details to webapi.xml
Create etc/webapi.xml in your module and add a route that names the URL, HTTP method, service interface, operation, and permission:
<route url="/V1/vendor/items/:id" method="GET">
<service class="VendorCatalogApiItemLookupInterface"
method="getById"/>
<resources>
<resource ref="VendorCatalog::itemsview"/>
</resources>
</route>The :id segment is a path parameter; Magento passes it to the service method when the request matches. Keep the declared method signature aligned with the route’s parameters, and choose a versioned path such as /V1/ to make future API changes easier to manage.
A route is only usable when Magento can find its concrete class and the caller has the required permission. Finish by wiring those two pieces together, then clear generated configuration after changing module files.
Configure dependency injection and access resources
In etc/di.xml, map the interface to its implementation with a preference:
<preference for="VendorCatalogApiItemLookupInterface"
type="VendorCatalogModelItemLookup"/>Define VendorCatalog::itemsview in etc/acl.xml and assign it to the appropriate role or integration. For a deliberately public endpoint, use Magento’s anonymous resource instead; avoid broad access for operations that expose private or store-management data.
Test and maintain your custom REST API
Your route is wired up; now the quickest way to catch a mismatch is to test it as a client would. A successful response confirms more than the URL—it also shows Magento can resolve the service and authorize the request.
Clear caches and verify the route with a REST client
Begin with a clean configuration cache so Magento loads your updated route and dependency-injection settings. Then send a request to the store’s REST base URL, including the store code if your setup requires one.
For the example lookup route, a request might look like this:
curl -i -H "Authorization: Bearer <access-token>"
https://example.com/rest/V1/vendor/items/123Use GET without a request body for this endpoint, and inspect both the HTTP status and response body. A successful response should contain the expected item data; test an ID that does not exist as well, so you can confirm how the service handles missing records.
After editing module configuration, run bin/magento cache:clean config. If you changed module setup or generated code, follow your deployment process for commands such as setup:upgrade or setup:di:compile rather than running them indiscriminately in production.
Troubleshoot common errors and permission issues
If the call fails, the status code and Magento’s response usually narrow down the cause. Check the request URL, method, token, and role before changing the service code.
A 404 can indicate a route path or HTTP-method mismatch, or that Magento has not loaded the latest configuration. A 401 commonly points to missing or invalid authentication, while a 403 often means the authenticated user or integration lacks the ACL resource. A 400 may signal invalid parameters or a request format the service does not accept.
For server-side failures, review Magento’s application and exception logs, then verify that the interface preference resolves to the intended implementation and that the method’s parameter and return types match the contract. Avoid exposing stack traces or sensitive details in client-facing error messages.
Keep Magento 2 REST API routes secure and compatible with future updates
Once testing passes, consider how the endpoint will behave as store data, permissions, and Magento versions change. Small safeguards now can prevent accidental data exposure and reduce breaking changes later.
Grant the route’s ACL resource only to roles that need it, use HTTPS, and avoid logging bearer tokens or sensitive payloads. Validate inputs in the service layer, and return only the fields the client requires; route configuration is not a substitute for business-level validation.
Finally, preserve the versioned URL and avoid changing an existing service contract in a way that breaks current clients. Add automated tests for authorization, valid and invalid inputs, and expected responses, then rerun them after Magento upgrades or module changes.
Build Magento 2 REST API routes with confidence
Creating Magento 2 REST API routes is about connecting a well-defined service contract to the right request, then making sure Magento can resolve and authorize it. With your module structure, route configuration, dependency injection, and access rules in place, you have the foundation for a reliable custom endpoint.
Test the route from a client’s perspective, use errors to guide troubleshooting, and keep security and compatibility in mind as your integration evolves. Start with a clear use case, build one endpoint at a time, and you’ll be ready to connect your Magento store with confidence.