The 23-time Grand Slam winner looked set to romp home when she seized the first set from Kanepi in just 18 minutes.
But the hard-hitting Estonian, who took out world number one Simona Halep in the first round, settled down in the second set, shaking off the partisan crowd in a jammed Arthur Ashe Stadium to push Williams the rest of the way.
"It wasn't an easy match at all," said Williams, who let out what she called a "Serena scream" after ripping a backhand crosscourt to hold serve in the opening game of the third set.
"Winning a big game and a very important game and a really tight game, I think it was just a relief," said Williams, who took control with a break in the next game and maintained that advantage until she fired a forehand winner on her first match point.
Williams finished with 18 aces and 47 total winners. A couple of loose points gifted Kanepi a break in the first game of the second set, however, and she couldn't get back on terms.
Williams double-faulted on break point to send Kanepi up 5-2. The American would win the next two games, but she couldn't find another break and after fighting off two set points saw Kanepi knot the match on her third chance.
"She's had a lot of big wins in her career. I was just happy to get through it, to be honest," Williams said.
Williams booked a quarter-final date with Karolina Pliskova, the eighth-seeded Czech who beat Australian Ashleigh Barty 6-4, 6-4.
Pliskova defeated Williams in the semi-finals in New York in 2016. Defending champion Sloane Stephens, the third seed, eased past Elise Mertens 6-3, 6-3 to book a quarter-final rematch with Latvian Anastasija Sevastova.
Sevastova toppled seventh-seeded Elina Svitolina 6-3, 1-6, 6-0, leaving just two of the top 10 women's seeds remaining.
Stephens, runner-up to Halep at the French Open, was looking forward to taking on Sevastova again in her bid to push ahead in her title defence.
"Obviously it won't be easy," she said. "Quarter-finals of a Grand Slam is always really tough. A big opportunity for both of us. Being defending champion, being able to get to the quarter-finals again, is incredible. I'm just going to try to keep building on that and keep going."
Sevastova, in the quarters for a third straight year, is hoping to turn the tables on Stephens, who is trying to become the first woman to win back-to-back US Open titles since Williams won three straight in 2012, 2013 and 2014.
"Maybe third time lucky for me," Sevastova said.
Stephens could be headed for a semi-final meeting with Williams, although Williams acknowledged that in Pliskova she'll face a player with plenty of weapons in addition to her big serve.
"She has a really good forehand. In fact, she doesn't do a lot of things bad," Williams said.
Plislkova's runner-up finish to Kerber in the 2016 US Open remains her best Grand Slam finish, but Williams noted she briefly gained the number one world ranking last year.
"She got there for a reason, she has a lot of strong parts to her game," Williams said. "It will be an interesting match." Pliskova was looking forward to it, too.
"I know she has a big game, but I have a big game too. I have a good serve. So I have some weapons, too. For sure, there is always a chance for me."
Williams heads into the second week at Flushing Meadows tracking two milestones: A seventh US Open title would see her break out of a tie with Chris Evert for the Open Era record and also equal Australian Margaret Court's all-time record of 24 major titles.
She hasn't won a major since the 2017 Australian Open. After returning to competition in March after the birth of daughter Olympia last September, Williams is trying to avoid her first season since 2011 without a Grand Slam title.