# nestjs-amo

> Wrapper over the @shevernitskiy/amo library for interacting with the amoCRM API from a NestJS application.

Latest version **0.1.0** (published 2024-10-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-amo
pnpm add nestjs-amo
yarn add nestjs-amo
bun add nestjs-amo
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2024-10-23 |
| First published | 2023-08-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 128.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Yaroslav Tolstoy |
| Maintainers | yatolstoy |
| Keywords | nestjs, amocrm, kommo, api, oauth2.0 |

## Links

- npm: https://www.npmjs.com/package/nestjs-amo
- Repository: https://github.com/yatolstoy/nestjs-amo
- Homepage: https://github.com/yatolstoy/nestjs-amo#readme
- Issues: https://github.com/yatolstoy/nestjs-amo/issues
- npm.io page: https://npm.io/package/nestjs-amo

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 0.1.0 (latest) — 2024-10-23
- 0.0.6 — 2023-10-30
- 0.0.5 — 2023-09-27
- 0.0.4 — 2023-09-11
- 0.0.3 — 2023-09-08
- 0.0.2 — 2023-08-28
- 0.0.1 — 2023-08-28
- 0.0.0 — 2023-08-28

## README

<h1 align="center"></h1>

<div align="center">
  <a href="http://nestjs.com/" target="_blank">
    <img src="https://nestjs.com/img/logo_text.svg" width="150" alt="Nest Logo" />
  </a>
</div>

<h3 align="center">nestjs-amo</h3>
<a href="https://www.npmjs.com/package/nestjs-nestjs-amo"><img src="https://img.shields.io/npm/v/nestjs-amo.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/nestjs-amo"><img src="https://img.shields.io/npm/l/nestjs-amo.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/nestjs-amo"><img src="https://img.shields.io/npm/dm/nestjs-amo.svg" alt="NPM Downloads" /></a>

<div align="center">
  <a href="https://nestjs.com" target="_blank">
    <img src="https://img.shields.io/badge/built%20with-NestJs-red.svg" alt="Built with NestJS">
  </a>
</div>

### Introduction

This is a simple wrapper of
[@shevernitskiy/amo](https://www.npmjs.com/package/@shevernitskiy/amo) library.
With this module you can interact with the amoCRM/Kommo API from a NestJS
application.

### Installation

```bash
# npm
npm install nestjs-amo

# yarn
yarn add nestjs-amo
```

Furthermore, install
[@shevernitskiy/amo](https://www.npmjs.com/package/@shevernitskiy/amo) in your
NestJS application, if you haven't already.

```sh
# npm
npm install @shevernitskiy/amo

# yarn
yarn add @shevernitskiy/amo
```

### Usage

Add `AmoModule` to the `imports` section in your `AppModule` or other modules to
gain access to `AmoService`.

#### Static import

```ts
import { Module } from "@nestjs/common";
import { AmoModule } from "nestjs-amo";

@Module({
	imports: [
		AmoModule.forRoot({
			isGlobal: true,
			amoServiceOptions: {
				widget_settings: {
					client_id: "1111-2222-3333",
					client_secret: "myclientsecret",
					redirect_uri: "https://myredirect.org",
				},
				getCredentials: async (amoId) => {
					// Implement your logic to retrieve a token from your long-term storage facility
					return {
						access_token: "some token",
						expires_at: 86400,
						expires_in: 1693211983,
						refresh_token: "saved token",
						domain: "https://example.ru",
					};
				},
				onTokenUpdate: async (amoId, token) => {
					// Implement your logic for saving authorization keys to long-term storage
					console.log("New token obtained", amoId, token);
				},
			},
		}),
	],
})
export class AppModule {}
```

Additionally, `AmoModule` provides a `forRootAsync` to pass options
asynchronously.

#### Async configuration

```ts
import { Module } from "@nestjs/common";
import { AmoModule } from "nestjs-amo";

@Module({
	imports: [
		AmoModule.forRootAync({
			isGlobal: true,
			useFactory: async () => {
				return {
					widget_settings: {
						client_id: "1111-2222-3333",
						client_secret: "myclientsecret",
						redirect_uri: "https://myredirect.org",
					},
					getCredentials: async (amoId) => {
						// Implement your logic to retrieve a token from your long-term storage facility
						return {
							access_token: "some token",
							expires_at: 86400,
							expires_in: 1693211983,
							refresh_token: "saved token",
							domain: "https://example.ru",
						};
					},
					onTokenUpdate: async (amoId, token) => {
						// Implement your logic for saving authorization keys to long-term storage
						console.log("New token obtained", amoId, token);
					},
				};
			},
		}),
	],
})
export class AppModule {}
```

You can inject dependencies such as `ConfigModule` to load options from .env
files.

```ts
import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { AmoModule } from "nestjs-amo";

@Module({
	imports: [
		ConfigModule.forRoot({
			isGlobal: true,
		}),
		AmoModule.forRootAync({
			isGlobal: true,
			useFactory: async (configService: ConfigService) => {
				return {
					widget_settings: {
						client_id: configService.get("AMO_CLIENT_ID"),
						client_secret: configService.get("AMO_CLIENT_SECRET"),
						redirect_uri: configService.get("AMO_REDIRECT_URI"),
					},
					getCredentials: async (amoId) => {
						// Implement your logic to retrieve a token from your long-term storage facility
						return {
							access_token: "some token",
							expires_at: 86400,
							expires_in: 1693211983,
							refresh_token: "saved token",
							domain: "https://example.ru",
						};
					},
					onTokenUpdate: async (amoId, token) => {
						// Implement your logic for saving authorization keys to long-term storage
						console.log("New token obtained", amoId, token);
					},
				};
			},
			inject: [ConfigService],
		}),
	],
})
export class AppModule {}
```

#### Calling send method

```typescript
import { AmoService } from "nestjs-amo";

@Injectable()
export class YourService {
	constructor(private amoService: AmoService) {}

	async method() {
		const amoApi = await this.amoService.create(123);
		return amoApi.account.getAccount({ with: ["version"] });
	}
}
```

#### Calling custom endpoint

```typescript
import { AmoService } from "nestjs-amo";

@Injectable()
export class YourService {
	constructor(private amoService: AmoService) {}

	async method() {
		const amoApi = await this.amoService.create(123);
		return amoApi.raw.get({
			url: "/ajax/merge/leads/save",
			headers: {
				"X-Requested-With": "XMLHttpRequest",
			},
			payload: "some payload",
		});
	}
}
```

## Contributing

Contributions welcome! See [Contributing](CONTRIBUTING.md).

## Notes

This project is not endorsed by or affiliated with
[AmoCRM](http://www.amocrm.ru).

## Author

**Yaroslav Tolstoy - [GitHub](https://github.com/yatolstoy)**

## License

Licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

---
_Source: https://npm.io/package/nestjs-amo · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
