* Erdogan party appears to be winning key cities Istanbul, Ankara
* Eight killed in clashes in southeast
* Test of Erdogan's popularity after scandals and leaks (Adds Erdogan party in early lead across country)
By Daren Butler and Humeyra Pamuk
ISTANBUL/ANKARA, March 30 (Reuters) - Tayyip Erdogan's ruling AKP party appeared on Sunday to be heading for a clear victory in local polls that have become a referendum on a prime minister facing corruption scandals and security leaks he blames on "traitors" embedded in state bodies.
With about half of votes counted, results cited on Turkish television put his Islamist-rooted AK Party ahead with 44-46 percent, the main opposition CHP on 23-28 percent. If that result were borne out, Erdogan might claim at least for now to have ridden out the biggest challenges to his 12-year rule.
Erdogan said the election results would reaffirm his legitimacy after a turbulent election campaign which has seen his government tighten its grip on the courts, purge thousands of police, and block access to Twitter and YouTube as it tried to stem a flow of graft allegations.
"Once the ballot boxes are opened, the rest is only footnotes to history," Erdogan said as he voted in Istanbul. "Today it is what the people say which matters rather than what was said in the city squares."
The races were close in the two biggest cities of Istanbul and Ankara, but the ruling party appeared to have the edge, with AKP supporters honking their car horns in celebration in the capital. Two buses of riot police meanwhile stood ready in Istanbul's plush Nisantasi neighbourhood, one of several districts rocked by anti-government protests last summer.
The AKP has set itself a goal of exceeding its 2009 result of 38.8 percent, an outcome likely to see Erdogan assert his authority even more strongly in a power struggle certain to continue after the polls.
A strong showing could embolden Erdogan to run for the presidency in an August election. Some fear it could also feed authoritarian trendencies and herald a period of harsh reckoning with opponents in politics and state bodies.
Erdogan has purged thousands of police and hundreds of judges and prosecutors since anti-graft raids in December targeting businessmen close to him and sons of ministers. He blames the raids on U.S.-based Islamic cleric Fethullah Gulen, an ex-ally who he says is using supporters in the police to try to topple the government.
"They are all traitors," Erdogan said of his opponents at a rally in Istanbul on the eve of the vote. "Let them do what they want. Go to the ballot box tomorrow and teach all of them a lesson ... Let's give them an Ottoman slap."
Voting passed off peacefully in most parts of the country, although clashes over local council positions killed eight in two separate shoot-outs in villages in the southeastern provinces of Hatay and Sanliurfa near the Syrian border.
Erdogan crisscrossed the nation of 77 million during weeks of hectic campaigning to rally his conservative core voters, in a measure of how seriously he takes his party's first test at the ballot box since last summer's protests across the country and the eruption of the corruption scandal in December.
"These were supposed to be local elections but in the last few weeks they've sort of become general elections," said Efe Yamac Yarbasi, a teaching assistant voting in Ankara.
Erdogan formed AK in 2001, attracting nationalists and centre-right economic reformers as well as religious conservatives who form his base. Since his 2011 poll victory he has in his statements moved more towards these core supporters.
The graft scandal, also involving anonymous Internet postings of tapped state communications implicating Erdogan in corrupt actions he denies, was all but eclipsed in recent days by the leaking of a recording of a top-level security meeting.
In the recording, the intelligence chief, foreign minister and military commanders discuss possible armed intervention in neighbouring Syria's civil war. A senior government official described the leak as one of the biggest crises in modern Turkey's history, threatening further sensitive disclosures.
Officials suggest Gulen's Hizmet network released the recording, giving an alarming sense in Ankara that government has only tentative control of state bodies and part of the security apparatus while power struggles play out.
Hizmet denies orchestrating the leak scandal and manoeuvring to control the state apparatus, but those close to the network say they fear a heavy crackdown after the elections. (Additional reporting by Seyhmus Cakan in Diyarbakir, Dasha Afanasieva in Hatay, Ayla Jean Yackley, Seda Sezer, Ece Toksabay, Can Sezer and Alexandra Hudson in Istanbul and Jonny Hogg and Gulsen Solaker in Ankara; editing by Ralph Boulton; Writing by Nick Tattersall; Editing by Ralph Boulton)
Our Standards: The Thomson Reuters Trust Principles.