# Using @dfinity/agent in node.js

**URL:** <https://forum.dfinity.org/t/using-dfinity-agent-in-node-js/6169>\
**Category:** Developers\
**Created:** [July 28, 2021, 12:46pm UTC](https://forum.dfinity.org/t/using-dfinity-agent-in-node-js/6169 "2021-07-28T12:46:12Z")\
**Posts on this page:** 1\
**Showing post:** 50

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [May 29, 2022, 7:49am UTC](https://forum.dfinity.org/t/using-dfinity-agent-in-node-js/6169/50 "2022-05-29T07:49:20Z")

</div>

Thanks to everyone above for the great commentary, really was nice to follow as I started troubleshooting not being able to load in my existing dfx principal via agent-js (I received the same different dfx and node principals as everyone else).

Since it did take me a bit of time to get through it all, hopefully these steps make it easier for those of you running into a similar issue (as of the time of this post).

### Steps

1. Navigate to my .dfx identities → `~/.config/.dfx/identity`
2. Create a new identity → `mkdir local-testing; cd local-testing`
3. Download [quill](https://github.com/dfinity/quill) since keysmith is now deprecated.
4. Test that quill is installed correctly → `quill`
5. Look up how to generate a key → `quill generate --help`
6. Generate a key and seed file → `quill generate --pem-file identity.pem --seed-file seed.txt`
7. Now implement Kyle’s code snippet below (copied from comment this post is replying to)

> [@anon74414410](#):
>
> ```auto
> const Identity = require("@dfinity/identity");
> const hdkey = require("hdkey");
> const fs = require("fs");
> 
> const { Secp256k1KeyIdentity } = Identity;
> 
> const bip39 = require("bip39");
> 
> const phrase = fs.readFileSync("seed.txt").toString().trim();
> 
> export const identityFromSeed = async (phrase) => {
> const seed = await bip39.mnemonicToSeed(phrase);
> const root = hdkey.fromMasterSeed(seed);
> const addrnode = root.derive("m/44'/223'/0'/0/0");
> 
> return Secp256k1KeyIdentity.fromSecretKey(addrnode.privateKey);
> };
> 
> identityFromSeed(phrase).then((identity) => {
> console.log(identity.getPrincipal().toString());
> });
> 
> ```

1. If you run this an have NodeJS version 17+, you will receive the following error coming from the hdkey library import → `Error: error:0308010C:digital envelope routines::unsupported`. The possible solutions for getting around this are [here](https://stackoverflow.com/questions/69692842/error-message-error0308010cdigital-envelope-routinesunsupported), the easiest of which is downgrading to node 16. (@anon74414410/sdk team can you provide a node 17+ solution? 🙂 )
2. Now running the above node script and `dfx identity get-principal` should return the same identity! Yay!

---

_[View the full topic](https://forum.dfinity.org/t/using-dfinity-agent-in-node-js/6169)._
