This is a simple wrapper of @shevernitskiy/amo library. With this module you can interact with the amoCRM/Kommo API from a NestJS application.
# npm
npm install nestjs-amo
# yarn
yarn add nestjs-amo
Furthermore, install @shevernitskiy/amo in your NestJS application, if you haven't already.
# npm
npm install @shevernitskiy/amo
# yarn
yarn add @shevernitskiy/amo
Add AmoModule
to the imports
section in your AppModule
or other modules to
gain access to AmoService
.
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.
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.
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 {}
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"] });
}
}
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",
});
}
}
Contributions welcome! See Contributing.
This project is not endorsed by or affiliated with AmoCRM.
Yaroslav Tolstoy - GitHub
Licensed under the MIT License - see the LICENSE file for details.