TL; TR (summary)
Whenever you need to program an interface for communication between two or more systems, you will have an API.
- A web service is an API that is used for communication between electronic devices through the web (originally with the XML format).
- An API is a programming interface (which is not always exposed to the web) and is not limited to a format for data transfer.
When to use an API and when a Web Service?
When the business requirement requires it or you consider it necessary.
What are web services?
Computer resources are its main objective is the communication between different teams and as the name suggests; through a network.
Such communication is achieved through the use of the XML format for data transfer.
What is an API?
Its acronym corresponds to the English Application Programming Interface (Application Programming Interface, in Spanish) and the API is a door within a system that allows us to interact with functionalities exposed through this interface.
Communication is not limited to a format like XML, it's free.
f. interface inform : Connection, physical or logical, between a computer and the user, a peripheral device or a communication link. - RAE .
Conclusions
A web service is an API, since it exposes the interface for two computers / systems to communicate, but as long as it is in XML.
On the other hand, an API is not always a web service, since it does not limit its communication to the XML format.
Exemplify:
(Interface) So that a stereo can emit sound, pulses are sent through the cable that leads to the speaker, but this cable is connected to the stereo. This connection (horn-stereo) is an interface and has been manufactured with certain industrial standards that will not guarantee its operation regardless of the manufacturer of the horn.
(Web service / API) A mobile application reports to a banking site the operations performed by a user in real time, its communication is through XML.
(JSON API) Another application is responsible for notifying the user of the browser (through alerts) about the new promotions of an X store, such communication is established by JSON format.
(API) A programmer performs an application to clean temporary data, cache, etc ... for Z operating system, but also offers more advanced features such as recovery of deleted files; for this, it uses (at a low level) the functions that the hard disk offers to achieve it (interface).
(Interface) JavaScript, CSS and HTML interact with each other thanks to the interface provided by the engine of each web browser.
If your communication will be in JSON, you will be building a JSON API and it will be called an exposed API, if it will be public or it will be accessed from the web (the word "exposed" does not mean that there is no access control system at the level logical).
If what you want is to implement architectures for the communication of your APIs, I recommend you see about:
- SOA: Software Oriented Architecture.
- ROA: Resource Oriented Architecture.
- REST: Respresentational Estate Transfer.
References: