Box

The Unofficial Box SDK for Elixir.

CircleCI

Installation

If available in Hex, the package can be installed by adding box to your list of dependencies in mix.exs:

def deps do
  [
    {:box, "~> 0.2.3"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/box.

Configuration

Box takes configuration from the application environment.

config :box,
  client_id: "abc12",
  enterprise_id: "123",
  public_key_id: "abc123",
  client_secret: "secret",
  private_key_path: "config/key.pem",
  private_key_password: "super secret"

Usage

Login

{:ok, auth} = BoxOAuth2.new()

Call login to authenticate with Box:

{:ok, auth} = BoxOAuth2.login(auth)

The access token will expire, so it is necessary to call login frequently. If the access token is still valid, login will return it as-is without reauthenticating.

Use the auth.client to perform actions against the API.

Uploading a file

Upload a local file to the root folder with a given name on Box.

{:ok, folder} = Box.folder_from_path(auth.client, "/")
{:ok, _response} = Box.upload(auth.client, "path/to/file.ext", folder.id, "file.ext")

Uploading may fail with an item_name_in_use error.

List files in a folder

{:ok, folder} = Box.folder_from_path(auth.client, "/")
{:ok, entries} = Box.folder_items(auth.client, folder.id, [:id, :name])
IO.inspect(entries)

Create a shared link

To share a file:

{:ok, file} = Box.file_from_path(auth.client, "file.ext")
{:ok, response} = Box.create_shared_link(auth.client, file.id)
IO.puts(response.shared_link.download_url)

To see an existing shared link (shared_link is nil if not created):

{:ok, file} = Box.file_from_path(auth.client, "file.ext")
{:ok, response} = Box.file_info(auth.client, file.id)
IO.puts(response.shared_link.download_url)