A customer selects a cupcake from the machine in Beverly Hills. (Damian Dovarganes/AP)

It’s 2:00 a.m. The bars have just closed, and you’re craving the sugary goodness of a cupcake. Trouble is, bakeries are closed, too. So your best bet is the pre-packaged Hostess snacks selling at 7 Eleven.

Not for much longer.

Come August, Sprinkles plans to turn on a 24-hour cupcake dispensing machine at its bakery, located at 3015 M St. NW.

Resembling an ATM, the machine features a touchscreen that allows customers to scroll through a menu of cupcake flavors. Once a selection is made and paid for by credit card, a robotic arm grabs the pastry from a shelf. The cupcake, neatly boxed up, is then dispensed through a turnstile.

Customers can only buy one cupcake at a time because of the configuration of the machine. Each dessert will set you back $4, meaning you pay 50 cents more than buying it in the store. Candace Nelson, founder of Sprinkles, said the surcharge is for the packaging.

The Beverly Hills cupcake ATM. (Courtesy of Sprinkles)

Bakers at Sprinkles plan to periodically restock the vault — which can hold 600 cupcakes — throughout the day to keep the pastries fresh. Nelson said the machine is a customer favorite in Beverly Hills, home of the bakery chain and where the first cupcake dispenser opened in March.

She estimates that the machine there doles out 1,000 cupcakes a day. At that rate, sales should more than cover the $100,000 cost of the machine any day now. Nelson is guarded about discussing finances, but said the machine has been a successful venture for the private company.

“It’s been a nice surprise for us,” said Nelson, who runs the company with her husband Charles. “We thought it would a fun innovative addition to our store, but we weren’t anticipating how much business it would drive.”

Nelson came up with the idea for the vending machine late one night when she was pregnant.

“All I wanted at the time was cupcakes, but nothing was open at that hour,” she said. “I thought ‘I own a cupcake shop and I can’t even get my hands on one. Just think of all of those pregnant women out there who would love a cupcake vending machine.’”

Nelson began working with a European company on a design for the machine. Sprinkles is installing the dispensers in nine markets across the country this summer, including New York, Chicago and Houston, where it has existing stores.

“We pay rent whether or not we’re open, so why not be able to sell to people 24-hours a day? We certainly don’t want to staff a store into the middle of the night, so this has been a nice way to bring in some additional revenue,” Nelson said.

The technology could allow the company to expand without opening additional brick-and-mortar locations, saving millions in build-out and operations costs.

“We could just use our production facility and restock the ATMs across town,” Nelson said. “It’s an interesting consideration for us, but we’re going to wait and see how the ATMs do before making any moves.”

In the meantime, Sprinkles is heading abroad with 34 stores planned in 10 countries in the Middle East, including Jordan, Egypt and Morocco. The company in June signed an agreement with M.H. Alshaya Co., a Kuwait-based franchise operator, to bring the bakery to the region. The first store is scheduled to open in Kuwait in December.

Nelson is not only venturing into new countries, but also new desserts with the May opening of Sprinkles Ice Cream in Beverly Hills. The store features classic selections such as rocky road and butter pecan, along with a variety of cupcake-inspired flavors such as red velvet.

Expanding the concept is top of the agenda for Nelson, who said, “there may be other things coming to D.C. aside from cupcakes.”

Sprinkles, which opened its first shop in 2005, is widely considered the forebear of the modern cupcakery, with its sleek packaging and couture cakes. The popular pastries helped Nelson land a gig as a judge on the Food Network’s “Cupcake Wars.”