SEATTLE — The Washington Supreme Court upheld Seattle’s so-called “gun violence tax” against a challenge from gun-rights groups Thursday.

In an 8-1 decision , the justices affirmed a lower court ruling that the levy was valid because it fell within the city’s taxing authority and because its primary purpose was to raise revenue for “the public benefit.”

The tax, which took effect last year, adds $25 to the price of each firearm sold in the city plus 2 or 5 cents per round of ammunition. It raised $200,000 in its first year, with the money earmarked for gun-violence research.

The National Rifle Association and other gun rights groups sued, along with gun stores and customers. They argued that under state law, the power to regulate firearms is by and large reserved to the state. Seattle’s measure was properly viewed as a regulation designed to hinder gun sales, not a tax, they argued.

In her opinion for the majority, Justice Debra Stephens disagreed.

State law “grants Seattle broad authority to tax retailers for the privilege of doing business within city limits,” she wrote.