A quick and honest apology has worked best in my experience
Amount of apology is case-by-case, person and context dependent
Some self deprecating humor goes a long way towards dispelling awkwardness
Generally this mistake is not a big deal, so continuing the conversation after the apology/recovery is a good way to smooth things out and make the slight quickly forgotten
As somebody who makes this slip up a lot in face to face conversations with people, my go-to strategy is immediately apologizing and trying to convey my embarrassment, probably with a laugh and some comment/excuse along the lines of "This happens to me with everybody, I'm terrible with names". After that I let it drop, and usually a quick laugh at my expense has patched any momentary offense the mistake may have caused. I feel that apologizing more than necessary can end up implying that a person should be more offended than they are, which doesn't lead anywhere good.
Most of this approach is directly transferrable to text communication, the caveat being you won't be able to gauge body language/facial expression right after the slip up to figure out the necessary level/intensity of the recovery. You'll have to make a judgement based on context and the potentially offended party, and their initial response to your mistake. If you and this person have a deep relationship and have been talking intimately about heavy/important things when you mess their name up, slightly more groveling and patching up may be needed than during smalltalk with an acquaintance. Acknowledging your mistake before they do can also go a long way towards minimizing offense caused.
Me: Well, I think it's time I head of to sleep
Friend (Clara): Well goodnight!
Me: Goodnight Alice!
Me: Whoops Dx, my bad Clara, maybe I should head to bed as well
In most cases a quick apology and a lighthearted conveyance of embarrassment will smooth things out best. Keeping the conversation moving past the mistake will also combat the potential awkwardness of dwelling on the mistake.
Me: Got a big day tomorrow?
In my experience, this is an extremely common mistake among people, so it rarely causes a lot of offense.
As an aside, I tend to avoid using peoples names in communication to avoid this exact issue, if you find this being a mistake you make often, changing your communication style to reduce the use of people's names is one way to just sidestep the problem entirely.