CODEF easyCodef-node Client
The `easycodef-node` library, currently at version 1.0.4, serves as a utility for integrating Node.js applications with the CODEF API services. It abstracts away common complexities associated with API interactions, such as automatic handling of access token issuance and reuse (which are valid for one week), and provides functionalities for secure data exchange using RSA encryption. The library supports managing Connected IDs for end-user account authentication with various financial institutions (banks, cards, insurance, etc.), although this step is optional for APIs not requiring a Connected ID. While the release cadence appears to be low, indicating stability, the library is actively maintained with recent patch updates addressing bug fixes. Key differentiators include its comprehensive support for CODEF's authentication mechanisms and streamlined API request process, contrasting with direct API calls that would require manual token and encryption management. It ships with TypeScript types, facilitating development in typed environments.
Common errors
-
Cannot find module 'easycodef-node' or its corresponding type declarations.
cause The package has not been installed or there's a module resolution issue in your environment.fixRun `npm install easycodef-node` in your project directory. For TypeScript, ensure `moduleResolution` in `tsconfig.json` is set appropriately (e.g., `NodeNext` or `Bundler`). -
Error: Invalid client information.
cause The provided `CLIENT_ID` or `CLIENT_SECRET` for the requested service type (demo/official) is incorrect or not registered with CODEF.fixDouble-check your client credentials against your CODEF account page. Ensure you are using the correct set of keys for the `SERVICE_TYPE` you are requesting (e.g., `DEMO_CLIENT_ID` for `SERVICE_TYPE_SANDBOX` or `SERVICE_TYPE_DEMO`). -
TypeError: codef.setPublicKey is not a function
cause The `codef` object is not an instance of `EasyCodef` or the `EasyCodef` class was imported incorrectly.fixEnsure you are creating an instance using `const codef = new EasyCodef();` and that `EasyCodef` is correctly imported from `easycodef-node`. -
Error: Required public key not set for encryption.
cause You are attempting to perform an operation or send data that requires RSA encryption, but `codef.setPublicKey()` has not been called or was called with an invalid key.fixProvide a valid RSA public key string to `codef.setPublicKey(PUBLIC_KEY)` before making API calls that involve encryption.
Warnings
- gotcha Failing to set correct client credentials (Client ID, Client Secret) for the target environment (demo, official) will result in authentication failures when making API requests.
- gotcha The RSA Public Key must be set via `setPublicKey` if any API requests involve encrypting sensitive user data (e.g., account details, certificates). Failure to do so will lead to encryption errors or rejected requests for sensitive operations.
- gotcha Connected IDs are specific to end-user account management and are not required for all CODEF API products. Attempting to use or manage Connected IDs unnecessarily can add complexity to your integration.
Install
-
npm install easycodef-node -
yarn add easycodef-node -
pnpm add easycodef-node
Imports
- EasyCodef
const EasyCodef = require('easycodef-node').EasyCodef;import { EasyCodef } from 'easycodef-node'; - EasyCodefConstant
const EasyCodefConstant = require('easycodef-node').EasyCodefConstant;import { EasyCodefConstant } from 'easycodef-node'; - EasyCodefUtil
const EasyCodefUtil = require('easycodef-node').EasyCodefUtil;import { EasyCodefUtil } from 'easycodef-node';
Quickstart
import { EasyCodef, EasyCodefConstant, EasyCodefUtil } from 'easycodef-node';
import path from 'path'; // Needed for EasyCodefUtil.encodeToFileString example if used
// Placeholder for CODEF client credentials (replace with your actual keys).
// Obtain these from the CODEF website after signing up for demo/official service.
const DEMO_CLIENT_ID: string = process.env.CODEF_DEMO_CLIENT_ID ?? 'YOUR_DEMO_CLIENT_ID';
const DEMO_CLIENT_SECRET: string = process.env.CODEF_DEMO_CLIENT_SECRET ?? 'YOUR_DEMO_CLIENT_SECRET';
const CLIENT_ID: string = process.env.CODEF_CLIENT_ID ?? 'YOUR_CLIENT_ID';
const CLIENT_SECRET: string = process.env.CODEF_CLIENT_SECRET ?? 'YOUR_CLIENT_SECRET';
// Placeholder for CODEF RSA Public Key (required for encryption).
// Obtain this from the CODEF website (e.g., https://codef.io/#/account/keys).
const PUBLIC_KEY: string = process.env.CODEF_PUBLIC_KEY ?? 'YOUR_PUBLIC_KEY';
// 1. Create an EasyCodef instance.
const codef = new EasyCodef();
// 2. Set the RSA Public Key for encryption.
// This is crucial for encrypting sensitive data sent to the CODEF API.
codef.setPublicKey(PUBLIC_KEY);
// 3. Set demo client information (for sandbox/demo environments).
codef.setClientInfoForDemo(DEMO_CLIENT_ID, DEMO_CLIENT_SECRET);
// 4. Set official client information (for production environment).
codef.setClientInfo(CLIENT_ID, CLIENT_SECRET);
// 5. Request an access token for the Sandbox environment.
// Note: easycodef-node automatically handles token issuance and reuse for API calls,
// so explicit token requests are usually only for specific scenarios or testing.
console.log('Requesting token for Sandbox...');
codef
.requestToken(EasyCodefConstant.SERVICE_TYPE_SANDBOX)
.then(function (response) {
console.log('Token 발급 결과 (Sandbox):');
console.log(response);
// Expected response for Sandbox token request:
// { data: { accessToken: "sandbox-access-token", ... } }
})
.catch(function (error) {
console.error('Error requesting token:', error);
});