Aircraft: COMMERCIAL FIXED WING
Phase of flight: Initial Climb
Anomaly: ATC Issue All Types; Conflict NMAC; Deviation - Altitude Excursion From Assigned Altitude; Deviation - Track / Heading All Types; Deviation / Discrepancy - Procedural Clearance

Flight took off at XA32Z and was on departure from Miami International airport; with the First Officer (FO) flying and the Captain (CA) operating as Pilot Monitoring (PM). The flight was given a 090 heading on departure until 2000 ft. when Departure Control (Frequency 119.45) directed a vector to the North and cleared the flight to climb to 7;000 ft. At XA35Z; the flight was level at 7;000 ft. in Instrument Meteorological Conditions (IMC) and Departure Control ordered 'Turn right to 090'. Read back the radio call and began an immediate right turn. While turning; Departure advised there was another aircraft somewhere at 11 o'clock and 1000 ft. below Aircraft X (which was unseen because of IMC). Immediately after making the traffic call; at XA36Z; Departure Control ordered an 'Immediate Left turn to 070'. Aircraft X rolled from a right hand turn into a left hand turn and attempted to comply; while the CA read back the instructions. While attempting to comply; just as the turn was reversing; Aircraft X was approximately over the KR66U fix and received a RESOLUTION ADVISORY (RA) that stated 'DESCEND!; DESCEND!'. The FO expeditiously put the aircraft in the RA window on the display; and correctly executed the RA maneuvers. The CA broadcast our RA compliance on frequency 119.45 and the TCAS indicated a contact immediately off our nose at our exact altitude. As Aircraft X descended through 6;850 ft. (shortly after the RA maneuver began); the TCAS aural alert 'Clear of contact' announced. The CA then transmitted to Miami's Departure in Frequency 119.45 that we were clear of contact and able to return to the 7;000 ft. and 070 heading as ordered. At this time; Departure cleared Aircraft X to climb and gave another course correction to a 090 heading. The flight continued without further incident. ATC vectored us into a near miss with another aircraft. Review routing/congestion issues in Miami Departure Control. Departed at XA32Z and was on departure from Miami International airport; with the First Officer (FO) operating as pilot flying (PF) and the Captain (CA) operating as Pilot Monitoring (PM). The flight was given a 090 heading on departure until 2000 ft. when Departure Control (Frequency 119.45) directed a vector to the north and cleared the flight to climb to 7;000 ft. At XA35z; the flight was level at 7;000 ft. in Instrument Meteorological Conditions (IMC) and Departure Control ordered 'Turn right;' to a specified heading. Aircraft X read back the radio call and began an immediate right turn. While turning; Departure advised there was another aircraft somewhere at 11 o'clock level at 6000 ft. Immediately after making the traffic call; at XA36Z; Departure Control ordered an 'Immediate Left turn to another specified heading. Aircraft X rolled from a right hand turn into a left hand turn. While in the left turn; there was a RESOLUTION ADVISORY (RA) that stated 'DESCEND!; DESCEND!'. The FO expeditiously put the aircraft in the RA window on the display; and correctly executed the RA maneuvers. The CA broadcast our RA compliance on frequency 119.45 and the TCAS indicated a contact immediately off our nose at our exact altitude. As Aircraft X descended through 6;850 ft. (shortly after the RA maneuver began); the TCAS aural alert 'CLEAR OF CONTACT' announced. The CA then transmitted to Miami's Departure in Frequency 119.45 that we were clear of contact and able to return to the 7;000 ft. The flight continued without further incident. ATC Error. Lots of traffic in the area at the time of the occurrence. De-congesting the airspace by spacing the arrivals and departures. Flight crew reported taking evasive action after receiving a TCAS RA while on departure.