@kosha-ai/integration-hubspot 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Codemancers
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,36 @@
1
- # Temporary Holding Version
1
+ # @kosha-ai/integration-hubspot
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ HubSpot provider for [Kosha](https://github.com/codemancers/kosha). Adds OAuth2
4
+ connect, automatic token refresh, an authenticated request proxy, and portal
5
+ identity for the HubSpot APIs.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @kosha-ai/integration-hubspot
11
+ ```
12
+
13
+ ## Use
14
+
15
+ Add it to your `kosha.config.ts`:
16
+
17
+ ```ts
18
+ import { defineConfig } from '@kosha-ai/server';
19
+ import hubspot from '@kosha-ai/integration-hubspot';
20
+
21
+ export default defineConfig({
22
+ // ...database, auth, encryption
23
+ integrations: [hubspot()],
24
+ });
25
+ ```
26
+
27
+ ## Scopes
28
+
29
+ The package asks for `crm.objects.contacts.read`, `crm.objects.companies.read`
30
+ and `crm.objects.deals.read`. An operator does not choose. Whoever connects
31
+ approves that set, and what HubSpot granted is stored against the connection.
32
+
33
+ A connection identifies the HubSpot **portal**, not the person who approved it.
34
+
35
+ See [docs/setup.md](./docs/setup.md) for creating the HubSpot app and for the
36
+ operation, scope and rate-limit matrix.
@@ -0,0 +1,2 @@
1
+ export { hubspot, hubspot as default } from './lib/integration-hubspot.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,8BAA8B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { hubspot, hubspot as default } from './lib/integration-hubspot.js';
@@ -0,0 +1,4 @@
1
+ import type { KoshaIntegration } from '@kosha-ai/integration-sdk';
2
+ export declare function hubspot(): KoshaIntegration;
3
+ export default hubspot;
4
+ //# sourceMappingURL=integration-hubspot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"integration-hubspot.d.ts","sourceRoot":"","sources":["../../src/lib/integration-hubspot.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAY,MAAM,2BAA2B,CAAC;AAgF5E,wBAAgB,OAAO,IAAI,gBAAgB,CA+C1C;AAED,eAAe,OAAO,CAAC"}
@@ -0,0 +1,109 @@
1
+ import { defineIntegration, oauth2, OAuth2Error, packageDirs, } from '@kosha-ai/integration-sdk';
2
+ const { docsPath, migrationsPath } = packageDirs(import.meta.url);
3
+ /**
4
+ * HubSpot versions its APIs by date. The v1 OAuth API stops working on
5
+ * 2027-02-16, so nothing here uses it.
6
+ */
7
+ const API_VERSION = '2026-09';
8
+ const API_BASE_URL = 'https://api.hubapi.com';
9
+ /**
10
+ * The CRM scopes HubSpot offers. Display and validation only, and limited to
11
+ * the CRM because that is what this package is for. Hand-maintained, so keep in
12
+ * sync with
13
+ * https://developers.hubspot.com/docs/apps/developer-platform/build-apps/authentication/scopes
14
+ */
15
+ const SCOPES_AVAILABLE = [
16
+ 'oauth',
17
+ 'crm.lists.read',
18
+ 'crm.lists.write',
19
+ 'crm.objects.companies.read',
20
+ 'crm.objects.companies.write',
21
+ 'crm.objects.contacts.read',
22
+ 'crm.objects.contacts.write',
23
+ 'crm.objects.custom.read',
24
+ 'crm.objects.custom.write',
25
+ 'crm.objects.deals.read',
26
+ 'crm.objects.deals.write',
27
+ 'crm.objects.line_items.read',
28
+ 'crm.objects.line_items.write',
29
+ 'crm.objects.owners.read',
30
+ 'crm.objects.quotes.read',
31
+ 'crm.objects.quotes.write',
32
+ 'crm.schemas.companies.read',
33
+ 'crm.schemas.companies.write',
34
+ 'crm.schemas.contacts.read',
35
+ 'crm.schemas.contacts.write',
36
+ 'crm.schemas.custom.read',
37
+ 'crm.schemas.deals.read',
38
+ 'crm.schemas.deals.write',
39
+ ];
40
+ /**
41
+ * Read-only on the three objects the migration names. Everything requested must
42
+ * also be selected on the HubSpot app, and a consumer that needs to write has
43
+ * to change this list.
44
+ */
45
+ const SCOPES_REQUESTED = [
46
+ 'crm.objects.companies.read',
47
+ 'crm.objects.contacts.read',
48
+ 'crm.objects.deals.read',
49
+ ];
50
+ /**
51
+ * HubSpot reports what it granted as a `scopes` array, where the default parser
52
+ * reads a `scope` string, so the grant would otherwise never be recorded.
53
+ */
54
+ function parseTokenResponse(body) {
55
+ const { access_token, refresh_token, token_type, expires_in, scopes } = body;
56
+ if (typeof access_token !== 'string') {
57
+ throw new OAuth2Error('Token response contained no access_token');
58
+ }
59
+ return {
60
+ accessToken: access_token,
61
+ refreshToken: typeof refresh_token === 'string' ? refresh_token : undefined,
62
+ tokenType: typeof token_type === 'string' ? token_type : undefined,
63
+ expiresInSeconds: typeof expires_in === 'number' ? expires_in : undefined,
64
+ scope: Array.isArray(scopes) ? scopes.join(' ') : undefined,
65
+ raw: body,
66
+ };
67
+ }
68
+ export function hubspot() {
69
+ return defineIntegration({
70
+ key: 'hubspot',
71
+ name: 'HubSpot',
72
+ docsUrl: 'https://developers.hubspot.com/docs/api-reference/latest/overview',
73
+ docsPath,
74
+ migrationsPath,
75
+ auth: oauth2({
76
+ authorizationUrl: 'https://app.hubspot.com/oauth/authorize',
77
+ tokenUrl: `${API_BASE_URL}/oauth/${API_VERSION}/token`,
78
+ tokenRequest: 'form',
79
+ scopeSeparator: ' ',
80
+ scopesAvailable: SCOPES_AVAILABLE,
81
+ scopesRequested: SCOPES_REQUESTED,
82
+ parseTokenResponse,
83
+ }),
84
+ api: {
85
+ baseUrl: API_BASE_URL,
86
+ authHeaders: ({ accessToken }) => ({
87
+ Authorization: `Bearer ${accessToken}`,
88
+ Accept: 'application/json',
89
+ }),
90
+ egress: [{ hosts: ['api.hubapi.com'] }],
91
+ },
92
+ identity: {
93
+ // Not token introspection, which needs the client secret and a strategy's
94
+ // identity step is not given one.
95
+ async fetchAccount({ accessToken, http }) {
96
+ const details = await http.get(`/account-info/${API_VERSION}/details`, { accessToken });
97
+ if (typeof details.portalId !== 'number') {
98
+ throw new Error('HubSpot account details contained no portalId');
99
+ }
100
+ const portalId = String(details.portalId);
101
+ return {
102
+ externalAccountId: portalId,
103
+ label: details.portalName ?? details.uiDomain ?? portalId,
104
+ };
105
+ },
106
+ },
107
+ });
108
+ }
109
+ export default hubspot;
Binary file
package/docs/setup.md ADDED
@@ -0,0 +1,143 @@
1
+ # HubSpot setup
2
+
3
+ Kosha needs a HubSpot app that uses OAuth. Creating one gives you the client ID
4
+ and secret a Kosha configuration asks for.
5
+
6
+ ## Create the app
7
+
8
+ HubSpot apps are created with the HubSpot CLI, as a small project you upload.
9
+ The account only needs developer tools, which any HubSpot account has.
10
+
11
+ ### 1. Find the projects page
12
+
13
+ In HubSpot, open **More**, then **Development**.
14
+
15
+ ![Open More](./images/hubspot-more-menu.png)
16
+
17
+ ![Open Development](./images/hubspot-development.png)
18
+
19
+ On **Projects**, click **Create project**. HubSpot shows the two CLI commands
20
+ used below.
21
+
22
+ ![Create project](./images/hubspot-create-project.png)
23
+
24
+ ![The CLI commands](./images/hubspot-cli-commands.png)
25
+
26
+ ### 2. Create the project with the CLI
27
+
28
+ ```bash
29
+ npm install -g @hubspot/cli
30
+ hs account auth
31
+ hs project create
32
+ ```
33
+
34
+ `hs account auth` asks for a personal access key, which HubSpot issues at
35
+ https://app.hubspot.com/l/personal-access-key. Answer `hs project create` with:
36
+
37
+ - **Project base:** App
38
+ - **Distribution:** Marketplace
39
+ - **Auth:** OAuth
40
+ - **Features:** none
41
+
42
+ ### 3. Set the redirect URL and scopes
43
+
44
+ In the new project, set the `auth` block of `src/app/app-hsmeta.json`. Use the
45
+ redirect URL exactly as the Kosha configuration page shows it:
46
+
47
+ ```json
48
+ "auth": {
49
+ "type": "oauth",
50
+ "redirectUrls": ["{publicUrl}/api/connect/callback/hubspot"],
51
+ "requiredScopes": [
52
+ "oauth",
53
+ "crm.objects.companies.read",
54
+ "crm.objects.contacts.read",
55
+ "crm.objects.deals.read"
56
+ ],
57
+ "optionalScopes": [],
58
+ "conditionallyRequiredScopes": []
59
+ }
60
+ ```
61
+
62
+ HubSpot requires `https` for redirect URLs, except `http://localhost`. `oauth` is
63
+ in HubSpot's own template and is fine to keep: HubSpot grants it without the
64
+ package asking. The three read scopes must match what the package requests.
65
+
66
+ Then upload it:
67
+
68
+ ```bash
69
+ hs project upload
70
+ ```
71
+
72
+ ### 4. Copy the client ID and secret
73
+
74
+ The project now appears under **Projects**. Open it, click the app, then the
75
+ **Auth** tab.
76
+
77
+ ![The project](./images/hubspot-project.png)
78
+
79
+ ![Open the Auth tab](./images/hubspot-app-overview.png)
80
+
81
+ Copy the **Client ID**, then click **Show** to copy the **Client secret**. Paste
82
+ both into a new HubSpot configuration in Kosha.
83
+
84
+ ![Client credentials](./images/hubspot-auth-credentials.png)
85
+
86
+ ### 5. Accept the Acceptable Use Policy
87
+
88
+ Until this is accepted, HubSpot refuses to install the app in any account other
89
+ than a developer test account. On the **Distribution** tab, click **Continue
90
+ publishing**, then agree to the policy.
91
+
92
+ ![Continue publishing](./images/hubspot-distribution.png)
93
+
94
+ ![Agree to the Acceptable Use Policy](./images/hubspot-acceptable-use.png)
95
+
96
+ The other steps in that panel are only for listing the app on HubSpot's
97
+ marketplace. An unlisted app can still be installed in up to 25 accounts.
98
+
99
+ ### 6. Connect from Kosha
100
+
101
+ Start the connect from Kosha. HubSpot's own install button on the
102
+ **Distribution** tab does not work here, because Kosha only accepts a login it
103
+ started.
104
+
105
+ ## Operations, scopes and rate limits
106
+
107
+ Paths are relative to `https://api.hubapi.com` and go through the Kosha proxy
108
+ unchanged. `{object}` is `contacts`, `companies` or `deals`.
109
+
110
+ Every CRM call needs `crm.objects.{object}.read` and counts against a limit of
111
+ 110 requests per 10 seconds, per portal, except search:
112
+
113
+ - **List:** `GET /crm/objects/2026-09/{object}?limit=&after=`
114
+ - **List archived:** `GET /crm/objects/2026-09/{object}?archived=true`
115
+ - **Read one:** `GET /crm/objects/2026-09/{object}/{id}`
116
+ - **Read with associated records:** `GET /crm/objects/2026-09/{object}/{id}?associations=`
117
+ - **Search:** `POST /crm/objects/2026-09/{object}/search`, which has a separate
118
+ limit and returns no rate-limit headers
119
+
120
+ Kosha also calls `GET /account-info/2026-09/details` when someone connects, to
121
+ identify the portal. It needs no scope beyond the three reads.
122
+
123
+ The 110 figure applies to marketplace-distributed apps. Check
124
+ [HubSpot's usage guidelines](https://developers.hubspot.com/docs/developer-tooling/platform/usage-guidelines)
125
+ for other distribution types.
126
+
127
+ ## Pagination and throttling
128
+
129
+ Kosha is a transparent proxy here. It forwards the query string, and returns
130
+ HubSpot's status, body and headers to the caller, so the caller owns both:
131
+
132
+ - **Pagination:** pass `paging.next.after` from a response back as `after`.
133
+ - **Throttling:** a limit reached is a `429` with a body carrying `errorType` and
134
+ `policyName`. Kosha does not retry it. Read `X-HubSpot-RateLimit-Remaining`,
135
+ `X-HubSpot-RateLimit-Max` and `X-HubSpot-RateLimit-Interval-Milliseconds` to
136
+ back off.
137
+
138
+ ## Tokens
139
+
140
+ Access tokens last 30 minutes, and Kosha refreshes them when they expire.
141
+
142
+ Revoking a connection in Kosha removes it from Kosha. It does not uninstall the
143
+ app from the HubSpot portal, which an admin does in HubSpot.
@@ -0,0 +1,48 @@
1
+ CREATE TABLE "hubspot_configuration" (
2
+ "id" TEXT NOT NULL,
3
+ "environmentId" TEXT NOT NULL,
4
+ "name" TEXT NOT NULL,
5
+ "slug" TEXT NOT NULL,
6
+ -- Encrypted, so TEXT rather than JSONB.
7
+ "credentials" TEXT,
8
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
9
+ "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
10
+
11
+ CONSTRAINT "hubspot_configuration_pkey" PRIMARY KEY ("id")
12
+ );
13
+
14
+ CREATE TABLE "hubspot_connection" (
15
+ "accountId" TEXT NOT NULL,
16
+ -- Copied from `account` so the unique index below can use it.
17
+ "userId" TEXT NOT NULL,
18
+ "environmentId" TEXT NOT NULL,
19
+ "configurationId" TEXT NOT NULL,
20
+ "metadata" JSONB,
21
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
22
+ "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
23
+
24
+ CONSTRAINT "hubspot_connection_pkey" PRIMARY KEY ("accountId")
25
+ );
26
+
27
+ CREATE UNIQUE INDEX "hubspot_configuration_environmentId_slug_key" ON "hubspot_configuration"("environmentId", "slug");
28
+
29
+ CREATE INDEX "hubspot_configuration_environmentId_idx" ON "hubspot_configuration"("environmentId");
30
+
31
+ CREATE UNIQUE INDEX "hubspot_connection_environmentId_configurationId_userId_key" ON "hubspot_connection"("environmentId", "configurationId", "userId");
32
+
33
+ CREATE INDEX "hubspot_connection_environmentId_idx" ON "hubspot_connection"("environmentId");
34
+
35
+ CREATE INDEX "hubspot_connection_configurationId_idx" ON "hubspot_connection"("configurationId");
36
+
37
+ CREATE INDEX "hubspot_connection_userId_idx" ON "hubspot_connection"("userId");
38
+
39
+ ALTER TABLE "hubspot_configuration" ADD CONSTRAINT "hubspot_configuration_environmentId_fkey" FOREIGN KEY ("environmentId") REFERENCES "environment"("id") ON DELETE CASCADE ON UPDATE CASCADE;
40
+
41
+ ALTER TABLE "hubspot_connection" ADD CONSTRAINT "hubspot_connection_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "account"("id") ON DELETE CASCADE ON UPDATE CASCADE;
42
+
43
+ ALTER TABLE "hubspot_connection" ADD CONSTRAINT "hubspot_connection_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE;
44
+
45
+ ALTER TABLE "hubspot_connection" ADD CONSTRAINT "hubspot_connection_environmentId_fkey" FOREIGN KEY ("environmentId") REFERENCES "environment"("id") ON DELETE CASCADE ON UPDATE CASCADE;
46
+
47
+ -- RESTRICT: a configuration that still has connections cannot be deleted.
48
+ ALTER TABLE "hubspot_connection" ADD CONSTRAINT "hubspot_connection_configurationId_fkey" FOREIGN KEY ("configurationId") REFERENCES "hubspot_configuration"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
package/package.json CHANGED
@@ -1,6 +1,56 @@
1
1
  {
2
2
  "name": "@kosha-ai/integration-hubspot",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.0",
4
+ "description": "HubSpot integration for Kosha",
5
+ "keywords": [
6
+ "kosha",
7
+ "hubspot",
8
+ "oauth",
9
+ "integrations"
10
+ ],
11
+ "license": "MIT",
12
+ "homepage": "https://github.com/codemancers/kosha#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/codemancers/kosha/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/codemancers/kosha.git",
19
+ "directory": "packages/integrations/hubspot"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "type": "module",
25
+ "main": "./dist/index.js",
26
+ "module": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ "./package.json": "./package.json",
30
+ ".": {
31
+ "@org/source": "./src/index.ts",
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.js",
34
+ "default": "./dist/index.js"
35
+ }
36
+ },
37
+ "files": [
38
+ "dist",
39
+ "docs",
40
+ "migrations",
41
+ "!**/*.tsbuildinfo"
42
+ ],
43
+ "dependencies": {
44
+ "tslib": "^2.3.0"
45
+ },
46
+ "kosha": {
47
+ "kind": "integration",
48
+ "key": "hubspot"
49
+ },
50
+ "peerDependencies": {
51
+ "@kosha-ai/integration-sdk": "0.5.4"
52
+ },
53
+ "devDependencies": {
54
+ "@kosha-ai/integration-sdk": "0.5.4"
55
+ }
6
56
  }