Skip to content

Latest commit

 

History

History
206 lines (166 loc) · 5.82 KB

File metadata and controls

206 lines (166 loc) · 5.82 KB

KBBI API - Usage Examples

Your KBBI API is now live at: https://kbbi-api.baguskto.workers.dev

Quick Test Commands

1. Check if word exists

curl https://kbbi-api.baguskto.workers.dev/api/lookup/rumah
# Response: {"exists":true,"word":"rumah"}

2. Get word details (meanings, word class, examples)

curl https://kbbi-api.baguskto.workers.dev/api/word/rumah
# Returns full KBBI entry with meanings, etymology, related words, etc.

3. Search for words

curl 'https://kbbi-api.baguskto.workers.dev/api/search?q=rum&limit=5'
# Response: {"query":"rum","count":5,"results":["rum","rum gula","ruma","rumah","rumah (ber)loteng"]}

4. Get typo suggestions

curl https://kbbi-api.baguskto.workers.dev/api/similar/rumh
# Response: {"word":"rumh","suggestions":[{"word":"ruah","distance":1},{"word":"rumah","distance":1},...]}

5. Check standard/non-standard form

curl https://kbbi-api.baguskto.workers.dev/api/check/rumah
# Response: {"is_standard":true,"word":"rumah","non_standard_forms":[]}

6. API Statistics

curl https://kbbi-api.baguskto.workers.dev/api/stats
# Response: {"total_words":112645,"api_version":"1.0.0","endpoints":[...]}

Integration with React Native

const KBBI_API_URL = 'https://kbbi-api.baguskto.workers.dev';

// Check if word exists
async function checkWord(word) {
  const response = await fetch(`${KBBI_API_URL}/api/lookup/${encodeURIComponent(word)}`);
  const data = await response.json();
  return data.exists;
}

// Get word details
async function getWordDetails(word) {
  const response = await fetch(`${KBBI_API_URL}/api/word/${encodeURIComponent(word)}`);
  return response.json();
}

// Get typo suggestions
async function getSuggestions(word, limit = 5) {
  const response = await fetch(
    `${KBBI_API_URL}/api/similar/${encodeURIComponent(word)}?limit=${limit}`
  );
  return response.json();
}

// Search words
async function searchWords(query, limit = 10) {
  const response = await fetch(
    `${KBBI_API_URL}/api/search?q=${encodeURIComponent(query)}&limit=${limit}`
  );
  return response.json();
}

// Check if word is standard form
async function checkStandardForm(word) {
  const response = await fetch(`${KBBI_API_URL}/api/check/${encodeURIComponent(word)}`);
  return response.json();
}

Example React Native Component

import React, { useState } from 'react';
import { View, TextInput, Text, FlatList } from 'react-native';

const KBBI_API_URL = 'https://kbbi-api.baguskto.workers.dev';

export default function SpellChecker() {
  const [text, setText] = useState('');
  const [suggestions, setSuggestions] = useState([]);

  const checkWord = async (word) => {
    if (!word) return;

    // Check if word exists
    const lookupRes = await fetch(`${KBBI_API_URL}/api/lookup/${encodeURIComponent(word)}`);
    const lookupData = await lookupRes.json();

    if (!lookupData.exists) {
      // Get suggestions for misspelled word
      const suggestRes = await fetch(`${KBBI_API_URL}/api/similar/${encodeURIComponent(word)}`);
      const suggestData = await suggestRes.json();
      setSuggestions(suggestData.suggestions);
    } else {
      setSuggestions([]);
    }
  };

  return (
    <View>
      <TextInput
        value={text}
        onChangeText={(value) => {
          setText(value);
          const words = value.split(' ');
          const lastWord = words[words.length - 1];
          checkWord(lastWord);
        }}
        placeholder="Type in Indonesian..."
      />

      {suggestions.length > 0 && (
        <FlatList
          data={suggestions}
          renderItem={({ item }) => (
            <Text onPress={() => {
              const words = text.split(' ');
              words[words.length - 1] = item.word;
              setText(words.join(' '));
              setSuggestions([]);
            }}>
              {item.word}
            </Text>
          )}
          keyExtractor={(item) => item.word}
        />
      )}
    </View>
  );
}

API Endpoints Summary

Endpoint Method Description
/ GET API information
/api/stats GET API statistics (total words, version, endpoints)
/api/lookup/:word GET Check if word exists in KBBI
/api/word/:word GET Get complete word details
/api/check/:word GET Check if word is standard form
/api/similar/:word?limit=N GET Get typo suggestions using Levenshtein distance
/api/search?q=query&limit=N GET Search words by prefix or substring

Performance

  • Global CDN: Deployed on Cloudflare's edge network (270+ locations worldwide)
  • Low Latency: KV storage with millisecond lookups
  • High Availability: 99.99% uptime SLA
  • Auto-scaling: Handles any traffic volume
  • Free Tier: 100,000 requests/day included

CORS

CORS is enabled for all origins (*), so you can use this API from:

  • React Native apps
  • Web browsers
  • Mobile apps
  • Any HTTP client

Next Steps for Your KBBI Assistant App

  1. Text Editor Screen: Use /api/lookup to check words as user types
  2. Spell Checking: Use /api/similar for typo suggestions
  3. Word Details: Use /api/word to show definitions, examples, etymology
  4. Autocomplete: Use /api/search for word suggestions
  5. Formality Checker: Use /api/check to detect non-standard forms

Monitoring & Management

# View real-time logs
npx wrangler tail

# Check KV storage
npx wrangler kv:key list --namespace-id=2649f25bbb964775bc357e0700eeec29

# Update a word
npx wrangler kv:key put "rumah" --path=word.json --namespace-id=2649f25bbb964775bc357e0700eeec29

# Deploy updates
npm run deploy

Data Stats

  • Total Words: 112,645
  • Non-standard Forms Indexed: 3,619
  • KV Storage Used: ~128 MB
  • Average Lookup Time: < 50ms globally

Enjoy building your KBBI Assistant app! 🚀