Visas are required by all except visitors from Belarus, China, Georgia, Iran, Kazakhstan, Kyrgyzstan, Moldova, Mongolia, Russia, Tajikistan, Turkey, Ukraine and Uzbekistan.
When applying for a visa you probably need an invitation from a state recognised tourist company. Rules vary depending on where you apply and are far from straightforward. Visas are valid for 30 or 90 days.
You may be able to obtain a transit visa that is valid for up to 5 days. There is no need for an invitation. Not all embassies issue transit visas.
Azerbaijan embassies and consulates - pdf file
Evidence of visiting Nagorno-Karabakh may result in refusal of entry.