When you first connect through the API, nextValidId is called, so you remember that number and use it on the next transaction. Each time you want a new ID, just increment it.
I never enter manual transactions when using my custom app so I don't know what happens to the ID numbers then.