How do I get permission to write a story about an established character?

I know there is fan fiction, but that doesn’t require permission.
Lets say I have an idea for a story centered around the original Captain Marvel. I pound out my novel or screenplay. How do I get it published without being sued? Do I have to buy the rights first or is that the job of an agent/publisher?