npm package: @jkt48/core
Description: Official JKT48 Connect API client for Node.js and browsers
Author: Valzyy
Latest version: 7.7.7

# JKT48 Connect API Client

Official JavaScript client for JKT48 Connect API. Works in Node.js and browsers (including React).

## Installation

```bash
npm install @jkt48/core
```

## Quick Start

### Node.js / CommonJS

```javascript
const JKT48Client = require('@jkt48/core');

const client = new JKT48Client({
  apiKey: 'YOUR_API_KEY'
});

// Get all members
const members = await client.members.getMembers();
console.log(members);
```

### ES6 Modules

```javascript
import JKT48Client from '@jkt48/core';

const client = new JKT48Client({
  apiKey: 'YOUR_API_KEY'
});
```

### React

```jsx
import { useState, useEffect } from 'react';
import JKT48Client from '@jkt48/core';

function App() {
  const [members, setMembers] = useState([]);
  
  useEffect(() => {
    const client = new JKT48Client({
      apiKey: 'YOUR_API_KEY'
    });
    
    client.members.getMembers()
      .then(data => setMembers(data))
      .catch(error => console.error(error));
  }, []);

  return (
    <div>
      {members.map(member => (
        <div key={member.id}>{member.name}</div>
      ))}
    </div>
  );
}
```

## Configuration

### Basic Configuration

```javascript
const client = new JKT48Client({
  apiKey: 'YOUR_API_KEY'
});
```

### With Priority Token

```javascript
const client = new JKT48Client({
  apiKey: 'YOUR_API_KEY',
  priorityToken: 'P-ABCD1234'
});
```

### With Admin Credentials

```javascript
const client = new JKT48Client({
  apiKey: 'YOUR_API_KEY',
  adminCredentials: {
    username: 'admin',
    password: 'password'
  }
});
```

## API Reference

### Members

```javascript
// Get all members
await client.members.getMembers();

// Get member detail
await client.members.getMemberDetail('Shani');

// Get birthdays
await client.members.getBirthdays();
```

### Live Streams

```javascript
// Get all current live streams
await client.live.getLive();

// Get YouTube live streams
await client.live.getYoutubeLive();

// Get IDN live streams
await client.live.getIdnLive();

// Get Showroom live streams
await client.live.getShowroomLive();

// Get recent streams
await client.live.getRecent();

// Get recent stream detail
await client.live.getRecentDetail('live123');

// Get replay streams
await client.live.getReplay();

// Get chat stream
await client.live.getChatStream('username', 'slug');

// Get Showroom chat stream
await client.live.getChatStreamShowroom('roomId');
```

### Events

```javascript
// Get all events
await client.events.getEvents();

// Get theater schedule
await client.events.getTheater();

// Get theater detail
await client.events.getTheaterDetail('theater123');

// Get video call schedules
await client.events.getVideoCall('sesi1', '2024-01-01', 'Shani');

// Get today's video call schedules
await client.events.getVideoCallToday();
```

### Media

```javascript
// Get YouTube videos
await client.media.getYoutube();

// Get news articles
await client.media.getNews();

// Get news detail
await client.media.getNewsDetail('news123');
```

### Admin (Requires Admin Credentials)

```javascript
// Get all API keys
await client.admin.getKeys();

// Get API key detail
await client.admin.getKeyDetail('key123');

// Create new API key
await client.admin.createKey('owner', 'email@example.com', 'premium');

// Update API key
await client.admin.updateKey('key123', true, 'premium');

// Delete API key
await client.admin.deleteKey('key123');

// Add limit to API key
await client.admin.addLimit('key123', 1000);

// Add expiry days to API key
await client.admin.addExpiry('key123', 30);

// Get admin statistics
await client.admin.getStats();
```

### Changelog

```javascript
// Get all changelogs
await client.changelog.getChangelogs();

// Get changelog detail
await client.changelog.getChangelogDetail('changelog123');

// Create new changelog
await client.changelog.createChangelog({ title: 'Update', content: 'New features' });

// Update changelog
await client.changelog.updateChangelog('changelog123', { title: 'Updated' });

// Delete changelog
await client.changelog.deleteChangelog('changelog123');
```

### Grow A Garden

```javascript
// Get stock information
await client.garden.getStock();

// Get weather information
await client.garden.getWeather();

// Get restock timer
await client.garden.getRestockTimer();

// Get all garden data
await client.garden.getAll();
```

### Utility Methods

```javascript
// Check API status
await client.check();

// Update API key dynamically
client.setApiKey('NEW_API_KEY');

// Update priority token dynamically
client.setPriorityToken('P-NEWTOKEN');

// Update admin credentials dynamically
client.setAdminCredentials({
  username: 'newadmin',
  password: 'newpassword'
});
```

## React Hooks Example

Create a custom hook for easier usage in React:

```jsx
import { useState, useEffect } from 'react';
import JKT48Client from '@jkt48/core';

const client = new JKT48Client({
  apiKey: process.env.REACT_APP_JKT48_API_KEY
});

export function useJKT48Members() {
  const [members, setMembers] = useState([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    client.members.getMembers()
      .then(data => {
        setMembers(data);
        setLoading(false);
      })
      .catch(err => {
        setError(err.message);
        setLoading(false);
      });
  }, []);

  return { members, loading, error };
}

// Usage in component
function MembersList() {
  const { members, loading, error } = useJKT48Members();

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error}</div>;

  return (
    <div>
      {members.map(member => (
        <div key={member.id}>{member.name}</div>
      ))}
    </div>
  );
}
```

## Error Handling

```javascript
try {
  const members = await client.members.getMembers();
  console.log(members);
} catch (error) {
  console.error('API Error:', error.message);
}
```

## Priority Token Usage

Priority tokens give your requests higher priority. You can add them in three ways:

1. **In client initialization** (recommended):
```javascript
const client = new JKT48Client({
  apiKey: 'YOUR_API_KEY',
  priorityToken: 'P-ABCD1234'
});
```

2. **Update dynamically**:
```javascript
client.setPriorityToken('P-ABCD1234');
```

The library automatically adds the priority token to:
- Request headers as `x-priority-token`
- Query parameters as `priority_token`
- Request body as `priority_token`

## License

MIT
## Official API Documentation

https://docs.jkt48connect.com