In Philadelphia last year, illegal fentanyl — a synthetic opioid once known mostly for its medicinal uses — was implicated in as many fatal overdoses as heroin, according to data from the city Health Department.

Of the 907 lethal overdoses, opioids caused 729. Toxicology tests detected fentanyl, the same opioid painkiller that killed the rock star Prince, in the blood of 413 of the victims, many of whom were using multiple substances.

"It's just crazy," said Special Agent Patrick J. Trainor of the federal Drug Enforcement Administration.

"When you graph the deaths involving fentanyl, the last three years have been like a hockey stick. Since 2014, it's really taken off."

Fentanyl is exponentially stronger than heroin. While a 30 milligram dose of heroin might kill an average-size man, it only takes about three milligrams of fentanyl— a few grains — to render the same result.

Most heroin users don't know if the product they have bought has been tainted with the synthetic drug or one of its chemically related cousins.

It is not always possible to tell exactly what killed a substance abuser, because toxicology tests often reveal numerous drugs, sometimes more than four, officials said. But they are especially alarmed at seeing so much more fentanyl turn up in tests.

"This is a very dangerous time for someone to be using drugs like these because there are so many unknowns," Trainor said. The number of fatal overdoses in Philadelphia last year was triple the number of homicides.

"Sometimes, if they find it's a better high, they go back and seek it out, not knowing it can be fatal." Williams said.

Nationwide, about 5,000 Americans died in 2014 with fentanyl in their blood, according to the federal Centers for Disease Control and Prevention. Though legal fentanyl is manufactured in the United States, and is commonly used in hospital operating rooms as an anesthetic and also as an analgesic for the worst kinds of pain, the illegal supply is created in underground labs in China and India.

Illicit fentanyl can be purchased on the internet and often is shipped directly, via the U.S. Postal Service, to a customer's doorstep. A consortium of lawmakers is trying to put a stop to that. In February, a bipartisan group in Congress introduced the Synthetics Trafficking and Overdose Prevention (STOP) Act.

It would require anyone shipping a package from abroad into the U.S. to declare in advance customs data, including its contents and who is sending it. Currently, only parcels carried by private carriers such as DHL and FedEx are required to provide that information.

"Bad guys seem to know that," said Tom Ridge, a former governor of Pennsylvania, who served as the first secretary of homeland security after 9/11.

Not all do, however.

In mid-March, state police in New Jersey seized a 30-pound shipment of fentanyl in Camden County. The package might not have been caught if it hadn't been shipped through a private carrier.

No one is expecting illegal shippers to declare if their packages are carrying the extraordinarily toxic drug. But any information can be useful to help customs officers flag suspicious packages, Ridge said.

"Right now, the U.S. mail doesn't have the advantage of running sophisticated algorithms to decide whether they'll pull suspect packages aside," Ridge said. "It's a glaring loophole.

"With this act, we might not eliminate them all, but it gives us a fighting chance."