How to get Blender's temp directory via the Python API?
Python's tempfile module.

First of all, Python has its own tempfile module, which is quite comprehensive and can likely do what you need(handles creating, temp files, directories and optionally - cleaning up when you're done).

Blender's User Preference

If you want to access the temp user preference, you can do so via.bpy.context.user_preferences.filepaths.temporary_directory
... which is initialized based on environment variables (See Environment Variables in the manual).

Blender's 'session' Temp Directory

However Blender crease a sub-directory within the temp directory, for each session, which is cleared on exit (even in the case of a crash).

And this is accessed via bpy.app.tempdir, in the case you describe, this sounds like it may be a good option.



Note that it may work well to use the tempfile module, even if you access Blender's temp directory using Blender's API.