Your friend code is issued at the town gate. Head to the building with the two dogs standing outside — that's Copper and Booker, the guards — and speak to the dog on the right.
He'll ask what you want to do. Choose the friend code option and he'll read it out for you.
Write it down, since you'll need to give it to anyone you want to visit, and they'll need to add yours in return before the connection works.