# Hook wants to call my API: how to authorize it?

**URL:** <https://community.auth0.com/t/hook-wants-to-call-my-api-how-to-authorize-it/50324>\
**Category:** Get Help\
**Tags:** api, hooks, authentication, auth0\
**Created:** [September 23, 2020, 10:34pm UTC](https://community.auth0.com/t/hook-wants-to-call-my-api-how-to-authorize-it/50324 "2020-09-23T22:34:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![catfood](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/catfood/32/24421_2.png) [@catfood](https://community.auth0.com/u/catfood)\
**Post date:** [September 23, 2020, 10:34pm UTC](https://community.auth0.com/t/hook-wants-to-call-my-api-how-to-authorize-it/50324/1 "2020-09-23T22:34:14Z")

</div>

Hey everyone. I have an SPA that authenticates with an Auth0 SPA app and calls a (homemade) API that accepts the SPA’s tokens. Works great. It’s exactly like the “ExampleCo” timesheet scenario in [Single-Page Applications (SPA) with API - Auth0 Docs](https://auth0.com/docs/architecture-scenarios/spa-api)

I want to add a hook to the SPA app, and that hook is going to need to call the same homemade API. Specifically, my API has its own User implementation, and I want to call something like `POST /api/user` whenever Auth0 creates a new user in the SPA app.

I’m trying this out and getting 401 (Unauthorized) failures in the hook testing window.

Note that this API is definitely accepting tokens directly from my SPA app. So I’m unclear on why a hook in my SPA app isn’t Authorized to call the API that is already known to accept tokens from my SPA app.

I realize that an M2M is recommended when you want to call an API from within a Hook, but… that would be _from_ what _to_ what under the circumstances? I’m confused by the need for any more complexity when I (should) already have the needed token.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 24, 2020, 9:33pm UTC](https://community.auth0.com/t/hook-wants-to-call-my-api-how-to-authorize-it/50324/3 "2020-09-24T21:33:39Z")

</div>

Hi @catfood,

Welcome to the Community!

To clarify, you mean you created a hook in auth0? Like a [post user registration hook?](https://auth0.com/docs/hooks/extensibility-points/post-user-registration)

If yes, then you would authenticate it with an M2M token. The hook acts as the client and your API as the resource.

Here is an explanation:

> **[Call Your API from a Machine-to-Machine App](https://auth0.com/docs/microsites/call-api/call-api-m2m-app)**
>
> Everything you need to know to call your API from your machine-to-machine (M2M) app

If you have questions please let me know!

---

<div class="post-metadata">

**Author:** ![catfood](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/catfood/32/24421_2.png) [@catfood](https://community.auth0.com/u/catfood)\
**Post date:** [September 25, 2020, 1:29am UTC](https://community.auth0.com/t/hook-wants-to-call-my-api-how-to-authorize-it/50324/4 "2020-09-25T01:29:50Z")

</div>

Yes, I created a post user registration hook in auth0. The thing I wasn’t getting (“more complexity”) is that hooks don’t belong to apps, they belong to the tenant. My hook doesn’t magically have access to the API just because they’re both defined in the same tenant. Okay.

I still feel like I’m writing a lot of code in my hook though. Do I have to go make a call to the /oauth/token endpoint and slog through all the parsing and error checking just to get the token that the M2M wants to give me? There has to be an easier way.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 25, 2020, 4:44pm UTC](https://community.auth0.com/t/hook-wants-to-call-my-api-how-to-authorize-it/50324/5 "2020-09-25T16:44:05Z")

</div>

> [@catfood](#):
>
> Do I have to go make a call to the /oauth/token endpoint and slog through all the parsing and error checking just to get the token that the M2M wants to give me?

Yes, to get an up-to-date token you have to request it from the token endpoint.

If you don’t want to write a function to get the token, then I would recommend checking out [this page](https://auth0.com/docs/flows/call-your-api-using-the-client-credentials-flow#example-post-to-token-url), where you can select a node.js example and all you have to do is paste the code in your hook and fill in credentials and domain. Fairly straightforward.

It’s also completely up to your how you authenticate the call to your API. The ability to write your own code gives you that flexibility.

---

<div class="post-metadata">

**Author:** ![catfood](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/catfood/32/24421_2.png) [@catfood](https://community.auth0.com/u/catfood)\
**Post date:** [September 25, 2020, 7:30pm UTC](https://community.auth0.com/t/hook-wants-to-call-my-api-how-to-authorize-it/50324/6 "2020-09-25T19:30:18Z")

</div>

Ah, that’s really clear now. Thanks, Dan! I was having trouble with the nomenclature that would help me find the best example.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [September 25, 2020, 8:20pm UTC](https://community.auth0.com/t/hook-wants-to-call-my-api-how-to-authorize-it/50324/7 "2020-09-25T20:20:30Z")

</div>

No problem! Let us know if you have any other questions. I am going to mark this resolved.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [October 10, 2020, 8:20pm UTC](https://community.auth0.com/t/hook-wants-to-call-my-api-how-to-authorize-it/50324/8 "2020-10-10T20:20:37Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
