I want to add an updated solution for QGIS 3 as some things have changed. I referenced various other StackOverflow threads and the QGIS Documentation page to make it work.
First, I'd like to mention this command that was hugely helpful to me. From the console you can run:
processing.algorithmHelp('gdal:cliprasterbymasklayer')
This prints out step-by-step instructions for the parameters, what values are accepted, etc. I used this to populate my parameter values.
Here is a code example:
import processing
from qgis.core import *
raster_layer = QgsRasterLayer('path to your input raster', 'raster')
mask_layer = QgsVectorLayer('path to your vector mask layer', 'mask', 'ogr')
parameters = {'INPUT': raster_layer,
'MASK': mask_layer,
'NODATA': -9999,
'ALPHA_BAND': False,
'CROP_TO_CUTLINE': True,
'KEEP_RESOLUTION': True,
'OPTIONS': None,
'DATA_TYPE': 0,
'OUTPUT': 'path to where you want to save'}
processing.runAndLoadResults('gdal:cliprasterbymasklayer', parameters)
A nice way to build your parameters list is to make a dictionary with them before feeding them into the GDAL tool.
Also, notice the processing.runandload method from QGIS 2 has now become processing.runAndLoadResults in QGIS 3.