How can we refresh or create Sandbox using API in Salesforce

INTRODUCTION

The objects ‘SandboxInfo’ and ‘SandboxProcess,’ are available in API v35 or higher, and allow you to create and refresh sandboxes. Tolloing api can be used to create or refresh the sandbox.

SandboxInfo
Represents a sandbox. SandboxInfo enqueues a sandbox for creation or for being refreshed. To learn more please review the Force.com Tooling API SandboxInfo section.

SandboxProcess
Represents the copy process of a sandbox once it is enqueued to get refreshed or created. To learn more please review the Force.com Tooling API SandboxProcess section.

EXAMPLE TO REFRESH A SANDBOX

  • Please note that you can refresh a sandbox by login in production only.
  • You must be admin to perform this step.
  • To start a sandbox refresh, edit/create the SandboxInfo record.
  • To find the status of a sandbox after it is enqueued, find the latest SandboxProcess record by querying SandboxProcess for a given SandboxInfoId value. The value of Status indicates the current state of the process.
  • When the Status field value is Pending Activation, change the value of the RefreshAction field to either ACTIVATE or DISCARD.

Query Sandbox

GET
/services/data/v52.0/tooling/query?q=SELECT+id,SandboxName,sourceid+from+sandboxinfo+where+SandboxName='UAT'

Initiate Refresh using PATCH method

PATCH
/services/data/v52.0/tooling/sobjects/SandboxInfo/0GQ58000000TQacGAG
Payload:
{
  "AutoActivate": true
}