Hansen was born in 1923 in Aberdeen, South Dakota, and grew up in Minneapolis, Minnesota, and Altadena, California.
Although he published almost forty books in a wide variety of genres, Hansen is best remembered for his ground breaking series of crime novels starring his most iconic creation, Dave Brandstetter, an openly homosexual private eye who still embodied the tough, no-nonsense personality of the classic hardboiled protagonist. His first adventure, Fadeout, was published in 1970, and over the next twenty-one years eleven more entries in the series were written: Death Claims (1973), Troublemaker (1975), The Man Everybody Was Afraid Of (1978), Skinflick (1979), Gravedigger (1982), Nightwork (1984), The Little Dog Laughed (1986), Early Graves (1987), Obedience (1988), The Boy Who Was Buried This Morning (1990), and A Country of Old Men (1991). No Exit Books, a British publisher, issued an omnibus volume, The Complete Brandstetter, in 2007 (ISBN 9781842431689).
Hansen published his first work, a poem, in The New Yorker, in 1952. He also published poetry in other magazines, briefly sang with a folk-music-group on a California radio-station, and had several part-time jobs in bookstores and magazines.
At the beginning of his career as a novelist, Hansen wrote under the pseudonym James Colton or James Coulton, producing novels such as Strange Marriage and Known Homosexual. He also wrote two gothic novels under the pseudonym Rose Brock.
Hansen was also noted for writing poems, teaching workshops, and hosting a 1960s radio-show called Homosexuality Today. In 1970, he helped to found the first Gay Pride Parade in Hollywood. Hansen disliked the term "gay" and always described himself as "homosexual".
Hansen won the 1992 Lifetime Achievement Award from the Private Eye Writers of America, as well as a Lambda Literary Award for Gay Men's Mystery from the Lambda Literary Foundation for A Country of Old Men: The Last Dave Brandstetter Mystery (1991).
Hansen was married to artist Jane Bancroft, a lesbian, from 1943 to her death in 1994. He said their relationship was that of "a gay man and a woman who happened to love each other." The couple had one daughter, who later had a sex-change operation. According to a friend quoted in an obituary, Hansen also had two long-term male lovers.
Hansen died from heart failure in 2004 at his home in Laguna Beach, California.