# Sentiment Analyzer (/docs/experiments/sentiment-analyzer)



Classify text sentiment as positive or negative using [Workers AI](https://developers.cloudflare.com/workers-ai/) and DistilBERT. Returns a label and confidence score for any short text input.

## Features [#features]

* Classify text as positive or negative with confidence scores
* Workers AI DistilBERT model at the edge
* Single GET endpoint with a `text` query parameter
* No external ML API keys required

## API Reference [#api-reference]

### GET /sentiment [#get-sentiment]

Analyzes the sentiment of the provided text.

<TypeTable
  type="{
  text: {
    description: &#x22;Text to classify (max 10,000 characters)&#x22;,
    type: &#x22;string&#x22;,
    required: true,
  },
}"
/>

#### Example Request [#example-request]

```bash
curl "https://your-worker.workers.dev/sentiment?text=This%20pizza%20is%20great!"
```

#### Success Response [#success-response]

**`text`** `string`

The original input text

**`label`** `string`

Sentiment label (e.g. `POSITIVE` or `NEGATIVE`)

**`score`** `number`

Confidence score for the predicted label (0–1)

```json
{
  "text": "This pizza is great!",
  "label": "POSITIVE",
  "score": 0.9998
}
```

#### Error Response [#error-response]

```json
{
  "error": "Missing or invalid query parameter: text",
  "code": "INVALID_TEXT"
}
```

#### Error Codes [#error-codes]

* `400` - Missing or invalid `text` (`INVALID_TEXT`)
* `502` - Workers AI model run failed (`AI_ERROR`)

## Use Cases [#use-cases]

* Score customer feedback and reviews at the edge
* Filter or route messages by sentiment in real time
* Learn Workers AI classification models in a stateless Worker
* Build moderation or analytics pipelines without external ML APIs

## Limitations [#limitations]

* Workers AI is subject to [usage limits](https://developers.cloudflare.com/workers-ai/platform/limits/) by plan
* Input text length is capped
* Accuracy varies on very short, sarcastic, or multilingual text

## Deployment [#deployment]

<Steps>
  <Step>
    ### Click the deploy button [#click-the-deploy-button]

    [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/shrinathsnayak/cloudflare-experiments/tree/main/apps/experiments/sentiment-analyzer)
  </Step>

  <Step>
    ### Deploy [#deploy]

    Enable the **Workers AI** binding (`AI`) in your Worker settings. The deploy button configures this automatically via `wrangler.json`. Requires a Cloudflare account with Workers AI enabled.
  </Step>

  <Step>
    ### Test your deployment [#test-your-deployment]

    ```bash
    curl "https://your-worker.workers.dev/sentiment?text=This%20pizza%20is%20great!"
    ```
  </Step>
</Steps>

## Local Development [#local-development]

```bash
cd apps/experiments/sentiment-analyzer
npm install
npm run dev
```

Test locally:

```bash
curl "http://localhost:8787/sentiment?text=This%20pizza%20is%20great!"
```

## Cloudflare Features Used [#cloudflare-features-used]

* **[Workers](https://developers.cloudflare.com/workers/)** - Edge compute runtime
* **[Workers AI](https://developers.cloudflare.com/workers-ai/)** - `@cf/huggingface/distilbert-sst-2-int8` sentiment classification model
