# Custom authentication

**URL:** <https://connect.superhuman.com/t/custom-authentication/32021>\
**Category:** Making Packs\
**Created:** [July 5, 2022, 6:00am UTC](https://connect.superhuman.com/t/custom-authentication/32021 "2022-07-05T06:00:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![andreysukhov90](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@andreysukhov90](https://connect.superhuman.com/u/andreysukhov90)\
**Post date:** [July 5, 2022, 6:00am UTC](https://connect.superhuman.com/t/custom-authentication/32021/1 "2022-07-05T06:00:21Z")

</div>

Hello, I’m working on creating a pack with authentication and fetching data from API, for successful response API requires custom authentication header, I’m quite confused about what to use for my case

right now it’s

for now authentication code is like this

```auto
pack.setUserAuthentication({
  type: coda.AuthenticationType.CustomHeaderToken,
  headerName: "Authorization",
  tokenPrefix: "Token",
  endpointDomain: "${ENDPOINT_DOMAIN}",
  instructionsUrl: "${INSTRUCTIONS_URL}",

  // Determines the display name of the connected account.
  getConnectionName: async function (context) {
    let url = "${API_URL}"
    let response = await context.fetcher.fetch({
      method: "GET",
      url: url,
    });
    return response.body.first_name;
  },
});

```

and I’ve got

```auto
 execute: async function ([id], context) {
      let secretHeader = "Authorization {{" + context.invocationToken + "}}";

      // I need header here, but in debug panel nothing seen

      let response = await context.fetcher.fetch({
      method: "GET",
      url: "${API_URL}",
      headers: { "Authorization": secretHeader}
    });
      let requestRes = response.body
      console.log(requestRes)
      let result = [];
      
      return {
        result: result,
      };
    },

```

how should I implement authentication and data fetching to add custom header to requests?

---

<div class="post-metadata">

**Author:** ![Eric\_Koleda](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/eric_koleda/32/50286_2.png) [@Eric\_Koleda](https://connect.superhuman.com/u/Eric_Koleda)\
**Post date:** [July 7, 2022, 12:36am UTC](https://connect.superhuman.com/t/custom-authentication/32021/2 "2022-07-07T00:36:20Z")

</div>

Hi @andreysukhov90 - Your authentication configuration looks correct, and it should cause the following header to be added to outgoing requests automatically:

```auto
Authorization: Token {USER_TOKEN}

```

As for you `execute` code, you shouldn’t need to set any headers at all. You seem to be using the pattern required for [`Custom` authentication](https://coda.io/packs/build/latest/guides/advanced/authentication/#custom-tokens), which is different than [`CustomHeader` authentication](https://coda.io/packs/build/latest/guides/advanced/authentication/#single-token).

I’d recommend removing the all of the authentication and header code from your `execute` function and see if it works.

---

<div class="post-metadata">

**Author:** ![andreysukhov90](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@andreysukhov90](https://connect.superhuman.com/u/andreysukhov90)\
**Post date:** [July 7, 2022, 11:38am UTC](https://connect.superhuman.com/t/custom-authentication/32021/3 "2022-07-07T11:38:37Z")

</div>

Hi Eric

tried it, but it didn’t work for me, headers are still not there

does customHeader strategy assumes that custom headers would automatically be added to each request? seems to me it doesn’t work like that

---

<div class="post-metadata">

**Author:** ![Eric\_Koleda](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/eric_koleda/32/50286_2.png) [@Eric\_Koleda](https://connect.superhuman.com/u/Eric_Koleda)\
**Post date:** [July 7, 2022, 12:37pm UTC](https://connect.superhuman.com/t/custom-authentication/32021/4 "2022-07-07T12:37:25Z")

</div>

> [@andreysukhov90](#):
>
> does customHeader strategy assumes that custom headers would automatically be added to each request?

Ya, that’s how all of our authentication schemes work. Here’s a sample Pack I created using `CustomHeader` auth:

```auto
import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();

pack.addNetworkDomain("httpbin.org");

pack.setUserAuthentication({
  type: coda.AuthenticationType.CustomHeaderToken,
  headerName: "Authorization",
  tokenPrefix: "Token",
});

pack.addFormula({
  name: "Test",
  description: "Test the authentication.",
  parameters: [],
  resultType: coda.ValueType.String,
  execute: async function ([], context) {
    let response = await context.fetcher.fetch({
      method: "GET",
      url: "https://httpbin.org/get",
    });
    let data = response.body;
    return JSON.stringify(data, null, 2);
  },
});

```

After I sign in and add it to a doc I get the following response:

```auto
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip,deflate",
    "Authorization": "Token 123abc",
    "Host": "httpbin.org",
    "User-Agent": "Coda-Server-Fetcher",
    "X-Amzn-Trace-Id": "Root=1-62c6d28d-2afaa64d57e456105042d6bc",
    "X-Fetcher-Request-Token": "djEtdjEtZkkrT0NFeFZrOXc4bFdZQWcweFpydz09LWgrKzMwV25iNCtRa1RJV0MtVVVVSm0wRDFuNmNIUE91QWt2b29RK0xKTGZGUVJsaDdCSzV5TnEvQ1BNaHV0djJmUVE2ZDBDcUNEYTlRcmE0blZZMD0="
  },
  "origin": "54.214.147.89",
  "url": "https://httpbin.org/get"
}

```

As you can see, the server is receiving my token in the `Authorization` header as expected. It’s also visible in the Pack logs:

![image](https://global.discourse-cdn.com/coda1/original/3X/b/6/b632ee4dafa90e33bf8cb740fb764ecaf466e824.png)

Give that code a try and see if you are seeing the same behavior.

---

<div class="post-metadata">

**Author:** ![andreysukhov90](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@andreysukhov90](https://connect.superhuman.com/u/andreysukhov90)\
**Post date:** [July 7, 2022, 2:52pm UTC](https://connect.superhuman.com/t/custom-authentication/32021/5 "2022-07-07T14:52:36Z")

</div>

I’ll try it out, thanks a lot

Andrey

чт, 7 июл. 2022 г. в 16:07, Eric Koleda via Coda Maker Community \<[notifications@coda1.discoursemail.com](mailto:notifications@coda1.discoursemail.com)\>:

---

<div class="post-metadata">

**Author:** ![andreysukhov90](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@andreysukhov90](https://connect.superhuman.com/u/andreysukhov90)\
**Post date:** [July 7, 2022, 7:20pm UTC](https://connect.superhuman.com/t/custom-authentication/32021/6 "2022-07-07T19:20:29Z")

</div>

It worked with addFormula but addSyncTable still doesn’t work for same reason, it doesn’t add custom header and returns 403 error because of it

---

<div class="post-metadata">

**Author:** ![andreysukhov90](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@andreysukhov90](https://connect.superhuman.com/u/andreysukhov90)\
**Post date:** [July 9, 2022, 5:39pm UTC](https://connect.superhuman.com/t/custom-authentication/32021/7 "2022-07-09T17:39:00Z")

</div>

is it any specific thing that i should do about addSyncTable that I miss? maybe you can give an advice how it works with authorization? sorry for hurry, got to complete the taks

пт, 8 июл. 2022 г. в 01:20, Андрей Сухов \<[andreysukhov90@gmail.com](mailto:andreysukhov90@gmail.com)\>:

---

<div class="post-metadata">

**Author:** ![Eric\_Koleda](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/eric_koleda/32/50286_2.png) [@Eric\_Koleda](https://connect.superhuman.com/u/Eric_Koleda)\
**Post date:** [July 10, 2022, 1:07am UTC](https://connect.superhuman.com/t/custom-authentication/32021/8 "2022-07-10T01:07:17Z")

</div>

I’m seeing it work fine in sync table as well:

```nohighlight
import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();

pack.addNetworkDomain("httpbin.org");

pack.setUserAuthentication({
  type: coda.AuthenticationType.CustomHeaderToken,
  headerName: "Authorization",
  tokenPrefix: "Token",
});

const MySchema = coda.makeObjectSchema({
  properties: {
    json: { type: coda.ValueType.String },
  },
  displayProperty: "json", // Which property above to display by default.
  idProperty: "json", // Which property above is a unique ID.
});

pack.addSyncTable({
  name: "MyTable",
  description: "",
  identityName: "Thing",
  schema: MySchema,
  formula: {
    name: "SyncThings",
    description: "",
    parameters: [],
    execute: async function ([], context) {
      let response = await context.fetcher.fetch({
        method: "GET",
        url: "https://httpbin.org/get",
      });
      let data = response.body;
      let json = JSON.stringify(data, null, 2);
      return {
        result: [{json: json}],
      };
    },
  },
});

```

![image](https://global.discourse-cdn.com/coda1/original/3X/e/1/e1d9ef74ee98d519ccddf26ce916971867645b2d.png)

Maybe it’s worth starting with code like that and then adapting it to your API?

---

<div class="post-metadata">

**Author:** ![andreysukhov90](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@andreysukhov90](https://connect.superhuman.com/u/andreysukhov90)\
**Post date:** [July 10, 2022, 9:46pm UTC](https://connect.superhuman.com/t/custom-authentication/32021/9 "2022-07-10T21:46:40Z")

</div>

Tried this, and it worked, many thanks to you!

вс, 10 июл. 2022 г. в 03:37, Eric Koleda via Coda Maker Community \<[notifications@coda1.discoursemail.com](mailto:notifications@coda1.discoursemail.com)\>:

---

<div class="post-metadata">

**Author:** ![system](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/system/32/24484_2.png) [@system](https://connect.superhuman.com/u/system)\
**Post date:** [October 8, 2022, 9:47pm UTC](https://connect.superhuman.com/t/custom-authentication/32021/10 "2022-10-08T21:47:34Z")

</div>

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