Skip to content

chalk-ai/chalk-elixir

Repository files navigation

Chalk Elixir

Dialyxir Integration tests Publish to Hex

Hex.pm Hexdocs.pm Hex.pm Hex.pm

Chalk Logo

Official Elixir client for Chalk Online Queries. This library provides an easy way to integrate with Chalk's API for feature computation and serving.

Installation

Add chalk_elixir to your list of dependencies in mix.exs:

def deps do
  [
    {:chalk_elixir, "~> 0.0.12"}
  ]
end

Usage

Basic Query

Online queries are executed by calling Chalk.Query.online/2 and passing inputs and expected outputs as described in the Chalk API documentation.

{:ok, response} = Chalk.Query.online(%{
  inputs: %{
    "user.id": 1
  },
  outputs: [
    "user.id",
    "user.email",
    "user.credit_score"
  ]
})

Advanced Options

The query can include additional options:

{:ok, response} = Chalk.Query.online(%{
  inputs: %{
    "user.id": 1
  },
  outputs: [
    "user.id",
    "user.email",
    "user.credit_score"
  ],
  staleness: %{
    "user.credit_score": "1d" # Allow data up to 1 day old
  },
  context: %{
    environment: "production",
    tags: ["high-priority"]
  },
  query_name: "user_profile_query" # For telemetry and observability
})

Response Structure

The response contains data, errors, and metadata:

%Chalk.Query.OnlineQueryResponse{
  data: [
    %Chalk.Query.FeatureResult{
      field: "user.id",
      value: 1,
      meta: %Chalk.Query.FeatureMeta{...}
    },
    %Chalk.Query.FeatureResult{
      field: "user.email",
      value: "[email protected]",
      meta: %Chalk.Query.FeatureMeta{...}
    },
    ...
  ],
  errors: [...],
  meta: %Chalk.Query.QueryMeta{...}
}

Authentication

The Chalk client supports two authentication methods:

Environment Variables

Set the following environment variables:

CHALK_CLIENT_ID=your_client_id
CHALK_CLIENT_SECRET=your_client_secret

Optionally, you can set a default deployment:

DEPLOYMENT_ID=your_deployment_id

Explicit Credentials

Pass credentials directly to the client:

Chalk.Query.online(
  %{
    inputs: %{
      "user.id": 1
    },
    outputs: [
      "user.id"
    ]
  },
  %{    
    client_id: "your_client_id",
    secret: "your_client_secret",
    deployment_id: "your_deployment_id" # Optional
  }
)

Credentials can be generated on the Chalk dashboard or via API.

Documentation

For detailed documentation, visit:

Development

Running Tests

mix test

Linting

mix dialyzer
mix credo

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.