Creating Notes

In this guide, we'll discuss how to create simple text notes as well as notes containing attachments using the Evernote Cloud API.

  • Creating a Simple Text Note

    The most basic Evernote note will contain only text. They can get much more complex than that, of course, but we need to crawl before we can walk.

    Prerequisites

    To proceed with this example, we’ll assume that the following are already in place:

    • You have a developer token (or a working OAuth implementation by which you can retrieve a valid authentication token)
    • You have an instance of UserStore and NoteStore

    Creating notes in Evernote is done using the NoteStore.createNote API function. This function takes two arguments: a valid auth token (as a string) and an instance of Types.Note. The Note instance, in turn, will need to have the following attributes defined at minimum before we can send it to the API:

    • Note.title — the title of the note
    • Note.content — the body of the note, formatted as ENML (Evernote Markup Language)

    Optionally, we can also set the GUID of the notebook where we’d like this note to reside (as Note.notebookGuid). If we don’t provide this value, the note will be created in the account’s default notebook.

    Each note’s body must begin with two things: the standard XML version declaration and the link to the ENML DOCTYPE definition for Evernote notes.

    The XML version declaration will look like this:

    <?xml version="1.0" encoding="UTF-8"?>

    The ENML DOCTYPE definition (DTD) for notes will look like this:

    <!DOCTYPE en-note SYSTEM "https://xml.evernote.com/pub/enml2.dtd">

    If either of these nodes is omitted in the note body, the Evernote API will reject the note as malformed and invalid.

    Assuming both of those pieces are in place, now we just need to add the body of the note, which must be wrapped in <en-note> tags. Here is the body of a simple (yet complete) Evernote note:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE en-note SYSTEM "https://xml.evernote.com/pub/enml2.dtd">
    <en-note>Hello, world!</en-note>

    If we take the above XML and a title, we have the necessary parts to create a very simple text note.

    Below you’ll find sample code that performs the entire note creation process.

  • Creating Notes with Attachments

    In addition to text, notes can also contain attachments (called “Resources” in the Evernote API). These files can be of any file type, but certain files will be represented differently.

    This section will describe how to add various attachments—audio, images, other files—to a note before it is created. First, let’s talk about the Resource data type (defined here).

    Types.Resource

    When we add a new Resource to a note, many of the members of this object will be set by the Evernote Cloud API when the resource is created: the Resource’s GUID, the GUID of the Note object containing the Resource, any recognition data as well as the Resource’s Update Sequence Number (USN).

    To take an ordinary file and make it into a Resource object, we need to perform the following steps:

    1. Determine the MIME type of the file to be attached.
    2. Read the contents of the file and use it to create an instance of Types.Data (which will then be assigned to the Resource object’s data member).
    3. Generate an MD5 hash of the contents of the file.

    This is the minimum amount of information necessary to attach a file to a Note, but we should add one last thing in the interest of completeness: the ResourceAttributes member. (Defined here), ResourceAttributes lets us define things like the filename, source URL and other data about the Resource.

    Here’s a simple function for turning a collection of files into Resource instances suitable for adding to an Evernote note:

    Creating a Resource doesn’t do us any good until we add it to a note, right? Embedding a Resource in a note requires the use of the <en-media> element, which is part of ENML. A Resource embedded in an ENML document might look something like this:

    <en-media type="image/jpg" hash="md5-of-file" />

    Image and PDF Resources

    The following MIME types can be displayed inline within the body of a note:

    • image/gif
    • image/jpeg
    • image/png
    • application/pdf

    When we include one of the above MIME types as a Resource in our note, we can (optionally) include width and height attributes in the <en-media> element:

    <en-media type="image/jpg" hash="md5-of-file" width="250" height="200" />

    Otherwise, the implementation is the same as described above.

    Audio Resources

    The MIME types supported for audio Resources are:

    • audio/wav
    • audio/mpeg
    • audio/amr

    When one of these file types is found in the type attribute of an <en-media> element, some Evernote clients will display an embedded audio player and allow playback of the file without leaving the app. The ENML syntax for audio Resources is the same as for all others; no special attributes or tags are required.

    All Other Resources

    Resources that aren’t images or audio files whose formats appear in the previous two sections will be displayed using what’s called “attachment view” (as opposed to “inline view”). This means that the attachment will appear as a small file icon in the body of the note. When the user clicks/taps the file icon, the file will be opened using whichever application the operating system thinks is the correct option for that file. In other words, Evernote itself won’t try to open the file.

  • Sample Code

    Here’s the same makeNote function we defined earlier, augmented to allow the attachment of Resources:

Stay on top of what's happening in the Evernote developer community.