This is what I do in my day job. International tax lawyer.

There are two taxes that you are talking about: income tax and Social Security tax.

First, income tax.

Because you live in Spain, you must pay income tax to Spain on your income. The source (a U.S. company) is irrelevant. You are a Spanish resident performing services in Spain.

Because you are a U.S. citizen you must pay income tax to the USA. The fact that you are living in Spain is irrelevant.

There are two ways to ease the pain:

- Foreign Earned Income Exclusion. See Form 2555 to determine if you qualify. If yes, the first $100,800 of earned income (be as careful with your definitions as you would be with the programming language of your choice) is not taxed by the USA, thus making the first $100,800 of earned income taxable only by Spain. You must file a U.S. tax return to get that benefit.

- Foreign Tax Credit. A dollar of income is at risk of tax twice: once by Spain, once by USA. The USA gives you a tax credit (see Form 1116) for tax you paid on that dollar of income. It isn't high precision math, but you can bludgeon the numbers to a close enough result.

- Use Both. You can use both. Use the Foreign Earned Income Exclusion for the first $100,800 of earned income, and the Foreign Tax Credit for earned income above that.

Second, Social Security tax.

Because you are working in Spain, you are supposed to contribute to the Spanish equivalent of Social Security.

You get around this by getting an exemption application via the totalization agreement between the United States and Spain. http://www.ssa.gov/international/Agreement_Pamphlets/spain.h...

This allows you to contribute to the U.S. system and not the Spanish system.

This exemption is typically good for 5 years with an option to extend to six years. Then you have to start contributing to the system where you live -- they figure if you've been camping out that long in Spain you must really be a resident there and therefore should contribute to the system.

Third, getting covered by the Spanish national health system.

Don't know. I did some stuff with Spain a little while ago and my memory is that if you contribute to the Spanish social security system you get the medical coverage and if you do not contribute to the Spanish social security system you don't get the medical coverage.

Fourth, what are you?

You are ambiguous about your relationship with the American startup. Are you an employee or an independent contractor? Either way the result should be the same (in the long run) but the paperwork required will be different (in the short run).

If you are an independent contractor, permit me to make a shameless plug: a recent blog post I did about how to be digital nomad, work abroad, and pay no Social Security tax. http://hodgen.com/how-digital-nomads-can-avoid-paying-social...

Fifth, talent and cost

There are very few people who know this stuff well. The ones that are out there tend to be (a) overworked; and (b) expensive. Sorry. Blame your government for emulating the Byzantine

Empire or medieval theologians for the complexity and burden imposed on you. If you do your tax work properly, you will probably only pay tax in Spain and pay nothing the USA. And to get there you will pay several thousand dollars a year in accounting fees.

My suggestion is that you practice Extreme Minimalism in your business affairs because you are far better off if you optimize for minimum tax advice expense first, then minimize tax second. :-)

Sixth, blowback

Let me just say that the biggest single part of our international tax law firm (and international tax is the ONLY thing we do) is expatriations. U.S. citizens giving up their citizenship.

This is especially true for Americans abroad and doubly true for American entrepreneurs abroad.

Disclaimer/warning shot/preemptive strike/we had to destroy the village in order to save it/etc.

I am an international tax lawyer. This is not legal advice to you because we've never met and I have no idea what is going on in your life. You'd be a damned fool to make important legal and tax decisions based on stuff you read on some random discussion board on the internet.

/Phil.