This Google Docs API page describes how to perform certain high-level tasks
involving Google Docs documents, such as:

- Create a document
- Copy an existing document

The following paragraphs describe these tasks in detail.

## Create a blank document

To create a document, use the
[`documents.create`](https://developers.google.com/workspace/docs/api/reference/rest/v1/documents/create)
method on the [`documents`](https://developers.google.com/workspace/docs/api/reference/rest/v1/documents)
collection.

The following code sample shows how to create a blank document with a specified
title:

### Java


```java
private static void createDoc(Docs service) throws IOException {
    Document doc = new Document()
            .setTitle("My Document");
    doc = service.documents().create(doc)
            .execute();
    System.out.println("Created document with title: " + doc.getTitle());
}
```

<br />

### Node.js


```javascript
const title = 'My Document';
const resource = {
  title,
};
this.service.documents.create({
  resource,
}, (err, response) => {
  if (err) return console.log('The API returned an error: ' + err);
  console.log(`Created document with title: ${response.data.title}`);
});
```

<br />

### PHP


```php
$title = 'My Document';
$document = new Google_Service_Docs_Document(array(
    'title' => $title
));

$document = $service->documents->create($document);
printf("Created document with title: %s\n", $document->title);
```

<br />

### Python


```python
title = 'My Document'
body = {
    'title': title
}
doc = service.documents() \
    .create(body=body).execute()
print('Created document with title: {0}'.format(
    doc.get('title')))
```

<br />

### Work with Google Drive folders

There's no option to create a document directly within a specified
Drive folder using the Docs API. By default, the
created document is saved to the user's root folder on Drive.

However, there are two alternatives to saving a file to a Drive
folder:

- After the document is created, move it to a specific folder using
  Drive API's
  [`files.update`](https://developers.google.com/workspace/drive/api/v3/reference/files/update) method. For
  more information on moving files, see [Move files between
  folders](https://developers.google.com/workspace/drive/api/guides/folder#move-files).

- Add a blank document to a folder using the Drive API's
  [`files.create`](https://developers.google.com/workspace/drive/api/v3/reference/files/create) method,
  specifying `application/vnd.google-apps.document` as the `mimeType`. For
  more information on creating files, see [Create a file in a specific
  folder](https://developers.google.com/workspace/drive/api/guides/folder#create-file).

For either alternative, you'll need to add the appropriate [Drive API
scopes](https://developers.google.com/workspace/drive/api/v3/reference/files/create#authorization-scopes) to
authorize the call. For more information on Drive scopes, see
[Choose Google Drive API scopes](https://developers.google.com/workspace/drive/api/guides/api-specific-auth).

To move or create a file within a shared drive folder, see [Implement shared
drive support](https://developers.google.com/workspace/drive/api/guides/enable-shareddrives).

## Copy an existing document

To copy a document, use Drive API's
[`files.copy`](https://developers.google.com/workspace/drive/v3/reference/files/copy) method.

Note that you can't use the `files.copy` method to copy a published document.
For more information, see [Published
documents](https://developers.google.com/workspace/docs/api/concepts/request-response#published-docs).

The following code sample shows how to copy an existing document. You can find
the ID to use for the Drive API call in the document URL. For more
information, see [Document
ID](https://developers.google.com/workspace/docs/api/concepts/document#document-id).

    https://docs.google.com/document/d/DOCUMENT_ID/edit

### Java


```java
String copyTitle = "Copy Title";
File copyMetadata = new File().setName(copyTitle);
File documentCopyFile =
        driveService.files().copy(DOCUMENT_ID, copyMetadata).execute();
String documentCopyId = documentCopyFile.getId();
```

<br />

### Node.js


```javascript
var copyTitle = "Copy Title";
let request = {
  name: copyTitle,
};
this.driveService.files.copy({
  fileId: DOCUMENT_ID,
  resource: request,
}, (err, driveResponse) => {
  let documentCopyId = driveResponse.id;
});
```

<br />

### PHP


```php

$copyTitle = 'Copy Title';
$copy = new Google_Service_Drive_DriveFile(array(
    'name' => $copyTitle
));
$driveResponse = $driveService->files->copy(DOCUMENT_ID, $copy);
$documentCopyId = $driveResponse->id;
```

<br />

### Python


```python
copy_title = 'Copy Title'
body = {
    'name': copy_title
}
drive_response = drive_service.files().copy(
    fileId=DOCUMENT_ID, body=body).execute()
document_copy_id = drive_response.get('id')
```

<br />

Note that you need to use an appropriate [Drive API
scope](https://developers.google.com/workspace/drive/v3/reference/files/copy#authorization-scopes) to
authorize the call. For more information on Drive scopes, see
[Choose Google Drive API scopes](https://developers.google.com/workspace/drive/api/guides/api-specific-auth).

## Related topics

- [Insert, delete, and move text](https://developers.google.com/workspace/docs/api/how-tos/move-text)
- [Merge text into a document](https://developers.google.com/workspace/docs/api/how-tos/merge)
- [Document concepts](https://developers.google.com/workspace/docs/api/concepts/document)