# Create custom api routes on the openEmr

**URL:** <https://community.open-emr.org/t/create-custom-api-routes-on-the-openemr/20149>\
**Category:** OpenEMR Development\
**Tags:** standard\_api\
**Created:** [March 15, 2023, 6:14pm UTC](https://community.open-emr.org/t/create-custom-api-routes-on-the-openemr/20149 "2023-03-15T18:14:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![williamneves](https://community.open-emr.org/user_avatar/community.open-emr.org/williamneves/32/17396_2.png) [@williamneves](https://community.open-emr.org/u/williamneves)\
**Post date:** [March 15, 2023, 6:14pm UTC](https://community.open-emr.org/t/create-custom-api-routes-on-the-openemr/20149/1 "2023-03-15T18:14:08Z")

</div>

Hi,

I’m trying to create new routes to the API, but I couldn’t get this working.

I made all the API client setup right, but when I tried to create new route, I get 401 error.

The same errors happens if I change any route name.

I could get working on version 6, but not at the version 7. Please some one help.

The code:

```auto
"GET /api/facility" => function () {
        RestConfig::scope_check("user", "facility", "read");
        RestConfig::authorization_check("admin", "users");
        $return = (new FacilityRestController())->getAll($_GET);
        RestConfig::apiLog($return);
        return $return;
    },

```

this is an default one and workfine, but if I change to `GET /api/facilit` just to test, I got the 401 response instead the 200.

---

<div class="post-metadata">

**Author:** ![adunsulag](https://community.open-emr.org/user_avatar/community.open-emr.org/adunsulag/32/12606_2.png) [@adunsulag](https://community.open-emr.org/u/adunsulag)\
**Post date:** [March 15, 2023, 8:04pm UTC](https://community.open-emr.org/t/create-custom-api-routes-on-the-openemr/20149/2 "2023-03-15T20:04:48Z")

</div>

Every api endpoint now has a corresponding scope in the ScopeRepository class on version 7. In your hypothetical **facilit** endpoint you would need a **user/facilit.read** scope to be able to access the endpoint. Endpoints are now checked against scopes in the access token automatically for the context of the user (patient or user) and the action (read for GET and write for PUT/POST/DELETE).

---

<div class="post-metadata">

**Author:** ![williamneves](https://community.open-emr.org/user_avatar/community.open-emr.org/williamneves/32/17396_2.png) [@williamneves](https://community.open-emr.org/u/williamneves)\
**Post date:** [March 16, 2023, 4:09pm UTC](https://community.open-emr.org/t/create-custom-api-routes-on-the-openemr/20149/3 "2023-03-16T16:09:21Z")

</div>

and where I add scopes?

---

<div class="post-metadata">

**Author:** ![adunsulag](https://community.open-emr.org/user_avatar/community.open-emr.org/adunsulag/32/12606_2.png) [@adunsulag](https://community.open-emr.org/u/adunsulag)\
**Post date:** [March 16, 2023, 4:34pm UTC](https://community.open-emr.org/t/create-custom-api-routes-on-the-openemr/20149/4 "2023-03-16T16:34:36Z")

</div>

Look at how I do it in my module skeleton example if you are doing this in a module:

> <https://github.com/adunsulag/oe-module-custom-skeleton/blob/774b1c83a610eaeb999553022dba930a8c27aac1/src/Bootstrap.php#L257>

If you are modifying core (which is not recommended) its in the ScopeRepository class as I mentioned earlier:

> <https://github.com/openemr/openemr/blob/9dc161d8d5b3fa71508b3e9f23b7c3e4aea49b97/src/Common/Auth/OpenIDConnect/Repositories/ScopeRepository.php#L28>

---

<div class="post-metadata">

**Author:** ![williamneves](https://community.open-emr.org/user_avatar/community.open-emr.org/williamneves/32/17396_2.png) [@williamneves](https://community.open-emr.org/u/williamneves)\
**Post date:** [March 16, 2023, 6:51pm UTC](https://community.open-emr.org/t/create-custom-api-routes-on-the-openemr/20149/5 "2023-03-16T18:51:25Z")

</div>

Man, I will enjoy your help and let you know some thing about what we are doing, to get the best pratices.

I’m creating some extra functionalities to opememr, but I’m using a external webapp to do this, so the best way is to get all my endpoints under openemr apis umbrella.

in that case is the reason i need the external access by apis, and was fine on 6.0, so now on 7 is needed to add scope to it.

So, I saw your bootstrap and I understand this code is to create new internals features right?

If I just wanna add a scope, can I use just the 257 func?

---

<div class="post-metadata">

**Author:** ![adunsulag](https://community.open-emr.org/user_avatar/community.open-emr.org/adunsulag/32/12606_2.png) [@adunsulag](https://community.open-emr.org/u/adunsulag)\
**Post date:** [March 16, 2023, 9:45pm UTC](https://community.open-emr.org/t/create-custom-api-routes-on-the-openemr/20149/6 "2023-03-16T21:45:28Z")

</div>

If you haven’t built a module or are not doing anything in a module then you won’t be able to listen to the events that get fired in the ScopeRepository class.

In your external APIs are you just adding everything directly to the \_rest\_routes.inc.php? If so you’ll need to go and modify the ScopeRepository class and add to the apiScopes() function or the fhirScopes() function.

Be aware that your code will likely break on future updates of OpenEMR as we modify the class. This is why its recommended you keep your changes in a module where you can listen to the ScopeRepository events and add your scopes when you receive the module event as demonstrated in my skeleton bootstrap file.

---

<div class="post-metadata">

**Author:** ![williamneves](https://community.open-emr.org/user_avatar/community.open-emr.org/williamneves/32/17396_2.png) [@williamneves](https://community.open-emr.org/u/williamneves)\
**Post date:** [March 17, 2023, 3:54pm UTC](https://community.open-emr.org/t/create-custom-api-routes-on-the-openemr/20149/7 "2023-03-17T15:54:56Z")

</div>

I have another question about the scopes.

I wanna make the following path on API

/api/module/submodule/getone  
/api/module/submodule2/getone

but if I create a scope user/module, I can access the /api/module/submodule, but can not the getone, give me a 401.

I need to add another scope, or change the scope for user/module/submodule?

---

<div class="post-metadata">

**Author:** ![williamneves](https://community.open-emr.org/user_avatar/community.open-emr.org/williamneves/32/17396_2.png) [@williamneves](https://community.open-emr.org/u/williamneves)\
**Post date:** [March 20, 2023, 1:23pm UTC](https://community.open-emr.org/t/create-custom-api-routes-on-the-openemr/20149/8 "2023-03-20T13:23:01Z")

</div>

Any advice for my last question?
