# Using Connect javascript API with npm/typescript

**URL:** <https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785>\
**Category:** Jira Cloud\
**Tags:** javascript-api, atlassian-connect\
**Created:** [April 24, 2023, 8:53pm UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785 "2023-04-24T20:53:37Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![opsb](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/opsb/32/47240_2.png) [@opsb](https://community.developer.atlassian.com/u/opsb)\
**Post date:** [April 24, 2023, 8:53pm UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/1 "2023-04-24T20:53:37Z")

</div>

The [Connect docs](https://developer.atlassian.com/cloud/jira/platform/about-the-connect-javascript-api/) demonstrate how you can run the API using a script tag

```auto
<script src="https://connect-cdn.atl-paas.net/all.js"></script>

```

I went looking for the library in npm and found [atlassian-connect-js - npm](https://www.npmjs.com/package/atlassian-connect-js) but it appears to have a different API. My attempt to get it working was

```auto
import AP from "atlassian-connect-js";

AP.context.getToken().then((token: any) => {
  console.log({token});
});

```

but `context` is undefined. How should I go about retrieving the token?

---

<div class="post-metadata">

**Author:** ![danielwester](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/danielwester/32/36730_2.png) [@danielwester](https://community.developer.atlassian.com/u/danielwester)\
**Post date:** [April 24, 2023, 8:56pm UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/2 "2023-04-24T20:56:30Z")

</div>

Because of security and “get you the latest version” reasons - you have to load all.js from [connect-cdn.atl-pass.net](http://connect-cdn.atl-pass.net). Loading locally could be done (not encouraged) - but you’d have to download it from [connect-cd.atl-paas.net](http://connect-cd.atl-paas.net) every once in a while.

/Daniel

---

<div class="post-metadata">

**Author:** ![opsb](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/opsb/32/47240_2.png) [@opsb](https://community.developer.atlassian.com/u/opsb)\
**Post date:** [April 24, 2023, 9:09pm UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/3 "2023-04-24T21:09:18Z")

</div>

Hey @danielwester, thanks for the pointer. Do you have any advice how to incorporate it into a typescript project? I’m a little confused because there’s [@types/atlassian-connect-js - npm](https://www.npmjs.com/package/@types/atlassian-connect-js) which has types matching the all.js API but I don’t see how they would be applied to the global AP.

---

<div class="post-metadata">

**Author:** ![opsb](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/opsb/32/47240_2.png) [@opsb](https://community.developer.atlassian.com/u/opsb)\
**Post date:** [April 25, 2023, 7:27am UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/4 "2023-04-25T07:27:44Z")

</div>

It turns out the issue was having both `atlassian-connect-js` and `@types/atlassian-connect-js` installed at the same time. Once I removed the `atlassian-connect-js` dependency the following started working with the globally defined AP from the `all.js` script.

```typescript
import { AP } from "atlassian-connect-js";

AP.context.getToken().then((token: any) => {
  console.log({token});
});

```

---

<div class="post-metadata">

**Author:** ![remie](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/remie/32/34985_2.png) [@remie](https://community.developer.atlassian.com/u/remie)\
**Post date:** [April 25, 2023, 7:42am UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/5 "2023-04-25T07:42:15Z")

</div>

I don’t think the [atlassian-connect-js](https://www.npmjs.com/package/atlassian-connect-js) package is meant to be used client-side. This is the server-side implementation running in the host product. Not sure why they chose to publish it on NPM 🤔

Keep in mind that the `@types/atlassian-connect-js` are community maintained, these are not official types. If you have questions about the typing, it’s best to contact @tobitheo.

---

<div class="post-metadata">

**Author:** ![opsb](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/opsb/32/47240_2.png) [@opsb](https://community.developer.atlassian.com/u/opsb)\
**Post date:** [April 25, 2023, 8:15am UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/6 "2023-04-25T08:15:58Z")

</div>

Hey @remie yes that does appear to be the source of the confusion here. `@types/atlassian-connect-js` is the types for `all.js` but the `atlassian-connect-js` package is, as you mention, intended for the backend so it has an entirely different API.

My instructions above turned out to be incomplete as they fixed the compile errors but resulted in a runtime error. To get it working I added

src/@types/global.d.ts

```auto
import AtlassianConnect from "atlassian-connect-js";

declare global {
  const AP: typeof AtlassianConnect.AP;
}

```

This makes the AP namespace globally available so you can just refer to it in typescript without an import. This assumes that you’ve made the AP global available by importing all.js using a script tag.

---

<div class="post-metadata">

**Author:** ![tobitheo](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/tobitheo/32/37087_2.png) [@tobitheo](https://community.developer.atlassian.com/u/tobitheo)\
**Post date:** [April 25, 2023, 8:31am UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/7 "2023-04-25T08:31:24Z")

</div>

What we tend to do is import the `@types/atlassian-connect-js` package in our `tsconfig.json`:

```json
{
  "compilerOptions": {
    "types": [
      "react",
      "jest",
      "atlassian-connect-js"
    ],
    ...
  },
  ...
}

```

This way, typescript will will pick up the globally defined types in the package without having to have `atlassian-connect-js` installed.

Also, @joshp actually started the package I believe, so he’s also a good person to talk to.

---

<div class="post-metadata">

**Author:** ![opsb](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/opsb/32/47240_2.png) [@opsb](https://community.developer.atlassian.com/u/opsb)\
**Post date:** [April 25, 2023, 9:18am UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/8 "2023-04-25T09:18:17Z")

</div>

Hey @tobitheo, I gave this a go but typescript isn’t able to resolve the global without the additional global declaration I had above. Looking at [DefinitelyTyped/index.d.ts at master · DefinitelyTyped/DefinitelyTyped · GitHub](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/atlassian-connect-js/index.d.ts) I don’t see it added to the global namespace so I’m not entirely surprised. I’ve never used a package that uses namespaces before though so I’m unfamiliar with the options around them. Maybe there’s a tsconfig option that adds namespaces to the global namespace automatically?

---

<div class="post-metadata">

**Author:** ![remie](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/remie/32/34985_2.png) [@remie](https://community.developer.atlassian.com/u/remie)\
**Post date:** [April 25, 2023, 10:42am UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/9 "2023-04-25T10:42:07Z")

</div>

We use this to make sure the namespace is globally available:

```auto
export {};

declare global {

  namespace AP {
  ...
  }
}

```

The reason for this is that the file is not included as a module unless it exports something, so it is just being ignored.

Also, it should not be required to add it to `types` in your tsconfig. From the TS documentation:

> By default all _visible_ ”`@types`” packages are included in your compilation. Packages in `node_modules/@types` of any enclosing folder are considered _visible_. For example, that means packages within `./node_modules/@types/`, `../node_modules/@types/`, `../../node_modules/@types/`, and so on.
> 
> If `types` is specified, only packages listed will be included in the global scope.

> **[TSConfig Reference - Docs on every TSConfig option](https://www.typescriptlang.org/tsconfig#types)**
>
> From allowJs to useDefineForClassFields the TSConfig reference includes information about all of the active compiler flags setting up a TypeScript project.

---

<div class="post-metadata">

**Author:** ![denis.zakharov](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/denis.zakharov/32/47367_2.png) [@denis.zakharov](https://community.developer.atlassian.com/u/denis.zakharov)\
**Post date:** [April 25, 2023, 8:03pm UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/10 "2023-04-25T20:03:08Z")

</div>

@opsb Thank you, this helped me a lot!

---

<div class="post-metadata">

**Author:** ![tobitheo](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/tobitheo/32/37087_2.png) [@tobitheo](https://community.developer.atlassian.com/u/tobitheo)\
**Post date:** [April 26, 2023, 12:28pm UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/11 "2023-04-26T12:28:32Z")

</div>

Interesting. I wonder why this is working for us… 😅

---

<div class="post-metadata">

**Author:** ![remie](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/remie/32/34985_2.png) [@remie](https://community.developer.atlassian.com/u/remie)\
**Post date:** [April 26, 2023, 12:37pm UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/12 "2023-04-26T12:37:36Z")

</div>

I think it is working because you have populated the `types` config parameter with the packages of which you expect global typings. However, this also means that when you add new `@types` packages with global scope, you will have to adjust your `tsconfig` file, whereas that would not be required if you did not specify the `types` config parameter.

It is even more interesting that you can actually achieve a combination of both by removing `types` and adding `include`. For instance, we use `@types` typings, but we also have our own private global typings in our private packages. In our `tsconfig` we do not specify `types`, but we do have:

```auto
  "include": [
    "./src/**/*.ts",
    "./typings/*.d.ts",
    "./typings/**/*.d.ts",
    "../node_modules/@collabsoft-net/**/*.d.ts"
  ],

```

which will make these typings available to us in our projects, including the global ones.

---

<div class="post-metadata">

**Author:** ![joshp](https://sea1.discourse-cdn.com/atlassiandeveloper/user_avatar/community.developer.atlassian.com/joshp/32/49514_2.png) [@joshp](https://community.developer.atlassian.com/u/joshp)\
**Post date:** [April 29, 2023, 12:03am UTC](https://community.developer.atlassian.com/t/using-connect-javascript-api-with-npm-typescript/68785/13 "2023-04-29T00:03:10Z")

</div>

Thanks for tagging me @tobitheo!

@opsb you’re totally correct that it’s confusing that the type definitions don’t exactly match the existing `atlassian-connect-js` package on NPM. When adding types to DefinitelyTyped, you are [required to name the types with the name of an existing npm package](https://github.com/DefinitelyTyped/DefinitelyTyped#create-a-new-package), and I came to the same conclusion as @remie that the `atlassian-connect-js` was the implementation inside Atlassian infrastructure, and that it was the most appropriate name for these type definitions.

I’ve just put together a sample repository which uses the global `AP` in a react project with needing to explicitly reference `@types/atlassian-connect-js` in case it’s useful to anyone:

> **[GitHub - josh-/atlassian-connect-types-example](https://github.com/josh-/atlassian-connect-types-example)**
>
> Contribute to josh-/atlassian-connect-types-example development by creating an account on GitHub.
