This section may contain outdated information.


Sessions help you store information about a user between requests. As long as the client supports cookies, sessions are easy to create.


Enable sessions on your Droplet by adding an instance of SessionMiddleware.

import Sessions

let memory = MemorySessions()
let sessions = SessionsMiddleware(sessions: memory)

Then add to the Droplet.

let drop = Droplet()

Note: If you'd like to enable or disable the middleware based on config files, check out middleware.


After SessionMiddleware has been enabled, you can access the req.sessions() method to get access to session data.

let data = try req.session().data


Let's create an example that remembers the user's name.


drop.post("remember") { req in
    guard let name = req.data["name"]?.string else {
        throw Abort.badRequest

    try req.session().data["name"] = Node.string(name)

    return "Remebered name."

On POST /remember, fetch a name from the request input, then store this name into the session data.


On GET /remember, fetch the name from the session data and return it.

drop.get("remember") { req in
    guard let name = try req.session().data["name"]?.string else {
        return throw Abort.custom(status: .badRequest, message: "Please POST the name first.")

    return name

The session will be stored using the vapor-session cookie.