# API call in Pre User Registration hook

**URL:** <https://community.auth0.com/t/api-call-in-pre-user-registration-hook/41584>\
**Category:** Get Help\
**Created:** [April 22, 2020, 6:58pm UTC](https://community.auth0.com/t/api-call-in-pre-user-registration-hook/41584 "2020-04-22T18:58:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DoubleBridges](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/doublebridges/32/20564_2.png) [@DoubleBridges](https://community.auth0.com/u/DoubleBridges)\
**Post date:** [April 22, 2020, 6:58pm UTC](https://community.auth0.com/t/api-call-in-pre-user-registration-hook/41584/1 "2020-04-22T18:58:56Z")

</div>

I’m trying to call our backend to get a Bearer Token in a Pre User Registration Hook in order to have access to it in my Apollo Client instance without making multiple calls and/or redirects.

```
module.exports = function (user, context, cb) {
  var response = {};
  var { request } = require('graphql-request');
  var API = "https://ourGraphqlEndpint/api";
  var query = `mutation {
     createUser(data: { email: $email, password: $password, username: $username }) {
        token
        user {
          id
        }
      }
      `;
      
  var variables = {
    email: user.email,
    password: user.password,
    username: user.username
  };
  
  var data= request(API, query, variables)
  .then(res => res.json())
  .then(data => {
    console.log(data)
    return data
  });

  response.user = user;
  response.user.user_metadata = { token: data.token }
 response.user.user_metadata = { our_id: data.id }

  cb(null, response);
};

```

This is my error

```
{
  "message": "Error when JSON serializing the result of the extension point",
  "statusCode": 500,
  "stack": "Error: Error when JSON serializing the result of the extension point\n at buildResponse (/data/sandbox/node_modules/auth0-ext-compilers/lib/adapter.js:115:41)\n at func (/data/sandbox/node_modules/auth0-ext-compilers/lib/compilers/user-registration.js:31:20)\n at module.exports (/data/io/d55b3914-58a5-4438-b5da-d3ba53cd1f5a/webtask.js:49:3)\n at Authz.is_authorized.error (/data/sandbox/node_modules/auth0-ext-compilers/lib/compilers/user-registration.js:30:16)\n at Object.is_authorized (/data/sandbox/node_modules/auth0-ext-compilers/lib/authorization.js:13:81)\n at userRegistrationHandler (/data/sandbox/node_modules/auth0-ext-compilers/lib/compilers/user-registration.js:9:18)\n at parseBody (/data/sandbox/node_modules/auth0-ext-compilers/lib/adapter.js:90:20)\n at finish (/data/sandbox/node_modules/auth0-ext-compilers/node_modules/wreck/lib/index.js:369:16)\n at wrapped (/data/sandbox/node_modules/auth0-ext-compilers/node_modules/hoek/lib/index.js:879:20)\n at module.exports.internals.Recorder.onReaderFinish (/data/sandbox/node_modules/auth0-ext-compilers/node_modules/wreck/lib/index.js:415:16)"
}

```

I’ve tried res.json and JSON.parse(res), but those are both DEserialization methods, so I’m not sure what to do about this. I’m also not sure if this is correct way to handle promises in this context.

---

<div class="post-metadata">

**Author:** ![DoubleBridges](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/doublebridges/32/20564_2.png) [@DoubleBridges](https://community.auth0.com/u/DoubleBridges)\
**Post date:** [April 22, 2020, 8:09pm UTC](https://community.auth0.com/t/api-call-in-pre-user-registration-hook/41584/2 "2020-04-22T20:09:45Z")

</div>

My query wasn’t properly structured, and I’ve fixed that

```
  var query = `mutation CREATE_USER($name: String!, $email: String!, $password: String!) {
      createUser(data: { name: $name, email: $email, password: $password }) {
        token
        user {
          id
          name
        }
      }
    }
      `;

```

And I am having problems with asnyc/promises but I will post that in a different question

---

<div class="post-metadata">

**Author:** ![DoubleBridges](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/doublebridges/32/20564_2.png) [@DoubleBridges](https://community.auth0.com/u/DoubleBridges)\
**Post date:** [April 22, 2020, 8:59pm UTC](https://community.auth0.com/t/api-call-in-pre-user-registration-hook/41584/3 "2020-04-22T20:59:14Z")

</div>

Figured it out. This was my first attempt at using the auth0 hooks, so I’m going to leave the working snippet here in case it can help out someone else. Although, I’m pretty sure that’s not acceptable error handling in the catch block.

```
module.exports = function (user, context, cb) {
  var response = {};
  var { request } = require('graphql-request');
  var API = "https://graphql.endpoint/api";
  var query = `mutation CREATE_USER($name: String!, $email: String!, $password: String!) {
      createUser(data: { name: $name, email: $email, password: $password }) {
        token
        user {
          id
          name
        }
      }
    }
      `;
      
  var variables = {
    email: user.email,
    password: user.password,
    name: user.username
  };
  
  response.user = user;
  
  async function createOurUser () {
    let newUser;

    try {
      newUser = await request(API, query, variables);

      var { createUser : { token, user: { name }}} = newUser;

      response.user.user_metadata.token = { token: token }
      response.user.user_metadata.name = { name: name }

      cb(null, response);

    } catch (err) {
      console.log(err)
    }

  }
  
  createOurUser()
};

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 23, 2020, 8:56am UTC](https://community.auth0.com/t/api-call-in-pre-user-registration-hook/41584/4 "2020-04-23T08:56:30Z")

</div>

Glad you have figured it out and thanks for sharing with the rest of community!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [April 23, 2020, 1:09pm UTC](https://community.auth0.com/t/api-call-in-pre-user-registration-hook/41584/5 "2020-04-23T13:09:12Z")

</div>


