How the Cupcake ATM became a $9 million chain

FORTUNE — An ATM that spits out cupcakes instead of cash doesn’t necessarily scream “genius business idea.” Nevertheless, the quirky invention is now filling a popular demand in the American market: late-night cravings for freshly baked sweets.

In six cities across the country including Chicago, Atlanta, and most recently New York City, sugar addicts can now purchase a cupcake for $4.25 from an automated machine 24 hours a day and seven days a week. The freshly baked goods are handcrafted by the California-based cupcake shop Sprinkles.

The novel idea came to the company’s co-founders Candace and Charles Nelson when Candace was pregnant with their first child. She thought it was ridiculous that she owned cupcake bakeries and couldn’t get a freshly baked treat at an odd hour, Charles told Fortune. That craving inspired the company’s first cupcake ATM in Beverly Hills, Calif. in 2012.

“When we created the first cupcake-only bakery in 2005, we thought, ‘Why can’t the cupcake be the star of the show?’,” says Charles. “What we saw is that we had customers who were demanding cupcakes 24 hours a day, but it doesn’t make sense to have your business open at three or four in the morning … fast-forward to the cupcake ATM.”

MORE: Red Robin is serving wine milkshakes. (Milkshakes. With wine.)

Charles, the cupcake empire’s CEO, wouldn’t go into specifics about the revenue earned from Sprinkles’ cupcake machines, but said each ATM serves about 1,000 cupcakes per day. By Fortune‘s calculations, if all six machines are selling roughly 365,000 cupcakes a a year for $4.25 each, Nelson has more than a $9 million business on his hands. The machine is so popular that when it debuted in Manhattan earlier this week, customers were lined up around the block to use it.

“It’s really good and fresh. It’s exquisite,” said 12-year-old Shane Leonard as he chomped into a black and white cupcake from the ATM. Shane’s sister Grace heard about the machine because it was trending on Facebook. The duo stood in line with their father and about 40 other cupcake devotees on Thursday afternoon.

Yet inventing the first cupcake ATM was not as easy as convincing customers to use it. When the Nelsons started designing the machine, they quickly realized nothing existed that could give a fully intact cupcake to a customer, says Charles. The company had to partner with a European firm to develop the technology that could deliver a cupcake without dropping it several feet like a typical vending machine. But once the prototype of the first cupcake ATM went to market in Beverly Hills, the machine couldn’t come close to handling the 1,000 transactions it received each day. “The machine was melting down, the parts were actually melting,” says Charles. After going through a second development cycle with the product, the ATMs can now adequately withstand growing customer demand and even disperse as many as four cupcakes at a time. The machine can also handle nearly 100 orders an hour thanks to faster customer transaction times.

“Shopping and dining is as entertainment-focused as ever,” says Charles. “You are still just buying and eating a cupcake, right? But I’ve watched customers on our original machine in Beverly Hills sit there and buy 12 cupcakes with 12 different transactions in a row because they wanted to see the robotic arm and the video play to go get it again and again.”

From a business perspective, the machine allow Sprinkles to basically do the impossible: increase hours of operation without raising costs. It is difficult to get employees to work odd hours during the night as well as dangerous from a security perspective, says Nelson. Now the company can sell cupcakes around the clock through the ATM without paying employees or putting them at risk. What’s more, is that Sprinkles can get away with charging $4.25 for cupcakes that come out of the machine as opposed to the $3.75 price seen in stores. In order for the baked goods to be delivered without damage, they had to come in a gift box that the company sells in its stores for an additional 50 cents.

“That’s cheap entertainment though,” explains Charles. “Since when has 50 cents changed your mind on anything?”

MORE: Quiznos and Potbelly: A tale of two sandwich chains

The Nelsons did not stumble upon this business boon completely by chance. Both Charles and Candace come from a background in investment banking in San Francisco. Before opening a cupcake bakery out of their home kitchen, the couple worked on initial public offerings with technology companies. Then they moved down to Los Angeles in 2003 with the idea of starting a cupcake-only bakery. At first, Candace and Charles couldn’t get anyone to lease them a space because the idea of a bakery that only sold cupcakes was too “kooky,” says Charles. Next thing they knew, celebrities like Tyra Banks and Henry Winkler were placing orders, and the business took off from there. The company is even partnering with a Middle Eastern franchise operator to open 34 new locations abroad.

Charles’ next big project is to put the infrastructure in place to support cupcake ATMs in locations that aren’t connected to Sprinkle’s 15 storefronts. The machines, while hold 760 cupcakes at a time, have to be stocked two to three times a day in order for the product to remain fresh, says Nelson. If executives can figure out how to do that in a cost-effective manner, the ATM model has limitless potential.

“My wife and I have always focused on innovation,” says Nelson. “The challenge of being an innovator is that you are always doing things for the first time. It’s more rewarding, but it’s harder.”

Subscribe to Well Adjusted, our newsletter full of simple strategies to work smarter and live better, from the Fortune Well team. Sign up today.