Files

Edit files with hunks

Changes text files without resending them. Each hunk names a file and replaces old, text that appears in it exactly once, with new. Hunks are applied in order, each against the result of the ones before it, and every file they touch is written as one saved version.

A hunk whose old matches nowhere byte for byte, but matches exactly one run of whole lines once leading indentation is ignored, is applied to those lines and new is re-indented to match.

Nothing is applied by halves: one hunk that cannot be applied fails the whole write with 400 BAD_USER_INPUT, field edits, and details.misses listing every miss (index, path, reason of NotFound, Ambiguous or NoSuchFile, and matches, lines or nearest to retry with). A set of hunks that changes nothing is refused.

patch/v2/pens/{id}/files

Request

  • Base URL
    https://api.codepen.io
  • Auth
    • HTTP bearer
    • or API key in header X-API-Key

Query parameters

waitinteger

Seconds to wait for the build (default 45, at most 75; 0 answers as soon as the files are saved). A wait in the JSON body takes precedence.

dry_runboolean

Build the proposed files without saving them: the head does not move and the answer's version has saved: false. A dry_run in the JSON body takes precedence.

Request body

dry_runboolean

Build the proposed files without saving them. The head does not move, and the answer's version has saved: false; it stays readable at GET /v2/versions/{id} for 36 hours.

expected_version_idstring

The saved_version.id the change was built on. The write is refused with 409 if the Pen's head has moved since.

waitinteger

Seconds to wait for the build (default 45, at most 75; 0 answers as soon as the files are saved).

labelstring

Names the saved version.

Response

The Pen, the newest version the write produced with its files, and that version's build.

Example response

{
  "build": {
    "files": [
      {
        "name": "app.js",
        "path": "/src/app.js"
      }
    ]
  },
  "pen": {
    "build": {
      "files": [
        {
          "name": "app.js",
          "path": "/src/app.js"
        }
      ]
    },
    "saved_version": {
      "build": {
        "files": [
          {
            "name": "app.js",
            "path": "/src/app.js"
          }
        ]
      },
      "files": [
        {
          "name": "app.js",
          "path": "/src/app.js"
        }
      ]
    }
  },
  "version": {
    "build": {
      "files": [
        {
          "name": "app.js",
          "path": "/src/app.js"
        }
      ]
    },
    "files": [
      {
        "name": "app.js",
        "path": "/src/app.js"
      }
    ]
  }
}

Changes

No recorded changes to this operation across all 1 revision of this API.